Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
5.2k views
in Technique[技术] by (71.8m points)

swift2 - Swift 2.0 : 'enumerate' is unavailable: call the 'enumerate()' method on the sequence

Just downloaded Xcode 7 Beta, and this error appeared on enumerate keyword.

for (index, string) in enumerate(mySwiftStringArray)
{

}

Can anyone help me overcome this ?

Also, seems like count() is no longer working for counting length of String.

let stringLength = count(myString)

On above line, compiler says :

'count' is unavailable: access the 'count' property on the collection.

Has Apple has released any programming guide for Swift 2.0 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Many global functions have been replaced by protocol extension methods, a new feature of Swift 2, so enumerate() is now an extension method for SequenceType:

extension SequenceType {
    func enumerate() -> EnumerateSequence<Self>
}

and used as

let mySwiftStringArray = [ "foo", "bar" ]
for (index, string) in mySwiftStringArray.enumerate() {
   print(string) 
}

And String does no longer conform to SequenceType, you have to use the characters property to get the collection of Unicode characters. Also, count() is a protocol extension method of CollectionType instead of a global function:

let myString = "foo"
let stringLength = myString.characters.count
print(stringLength)

Update for Swift 3: enumerate() has been renamed to enumerated():

let mySwiftStringArray = [ "foo", "bar" ]
for (index, string) in mySwiftStringArray.enumerated() {
    print(string)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...