Swift 2.0:"enumerate"不可用:在序列上调用"enumerate()"方法 [英] Swift 2.0 : 'enumerate' is unavailable: call the 'enumerate()' method on the sequence

查看:133
本文介绍了Swift 2.0:"enumerate"不可用:在序列上调用"enumerate()"方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只需下载Xcode 7 Beta,该错误就会出现在enumerate关键字上.

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

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

}

有人可以帮助我克服这个问题吗?

Can anyone help me overcome this ?

此外,似乎count()不再可以用于计算String的长​​度.

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.

Apple是否发布了任何有关Swift 2.0的编程指南?

Has Apple has released any programming guide for Swift 2.0 ?

推荐答案

许多全局函数已被协议扩展方法取代, Swift 2的新功能,因此enumerate()现在是扩展方法 对于SequenceType:

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>
}

并用作

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

并且String不再符合SequenceType,您必须 使用characters属性获取Unicode的集合 人物.另外,count()是的协议扩展方法 CollectionType而不是全局函数:

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)

Swift 3的更新:enumerate()已重命名为enumerated():

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

这篇关于Swift 2.0:"enumerate"不可用:在序列上调用"enumerate()"方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆