问题描述
只需下载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 :
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 methodfor 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 touse the characters
property to get the collection of Unicodecharacters. Also, count()
is a protocol extension method ofCollectionType
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()"方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!