简体   繁体   English

“类型“()”不符合协议“序列类型”

[英]“Type ”()“ does not conform to protocol ”SequenceType“”

I am following an online class but my professor is using a different version of Xcode. 我正在参加在线课程,但是我的教授正在使用其他版本的Xcode。 I have version 7.2.1. 我有7.2.1版。 It asked me to put parentheses after arr in the second line, but when I do, it says "Type "()" does not conform to protocol SequenceType . I don't know how to fix it. Thanks. 它要求我在第二行中的arr后面加上括号,但是当我这样做时,它说“类型“()”不符合协议SequenceType ,我不知道如何解决它。

var arr = {1; 2; 3; 4}    
for i in arr() {    
    print(i)
}

Your array declaration needs to use brackets and commas. 您的数组声明需要使用方括号和逗号。

var arr = [1, 2, 3, 4]

for i in arr {
    print(i)
}

Another way to write the same way is using below:- 编写相同方法的另一种方法是使用以下方法:

half-open range operator (..<) 半开范围运算符(.. <)

let arr = [1, 2, 3, 4]
for i in 0..<arr.count 
{
 print(arr[i])
}

closed range operator (...) 近距离运算符(...)

let arr = [1, 2, 3, 4]
for i in 0...arr.count-1 
{
 print(arr[i])
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM