错误:' ...'不是前缀一元运算符

时间:2017-07-05 08:48:41

标签: swift for-loop operator-keyword prefix

代码:

let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names[...2] {
    print(name)
}

错误:

Playground execution failed: error: MyPlayground.playground:3:19: error: '...' is not a prefix unary operator
for name in names[...2] {

我是swift的新手,因此任何相关链接或任何形式的帮助都将受到高度赞赏。使用swift 4

1 个答案:

答案 0 :(得分:1)

正如Martin和其他人所说,这在Swift 4中是有效的。在早期版本中,你可能想要使用它:

let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names[0..<3] {
    print(name)
}