gpt4 book ai didi

swift 2.0 : 'enumerate' is unavailable: call the 'enumerate()' method on the sequence

转载 作者:行者123 更新时间:2023-11-28 09:01:58 25 4
gpt4 key购买 nike

刚刚下载了 Xcode 7 Beta,这个错误出现在 enumerate 关键字上。

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

}

谁能帮我克服这个问题?

此外,似乎 count() 不再用于计算 String 的长度。

let stringLength = count(myString)

在上面一行,编译器说:

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

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

最佳答案

许多全局函数已被协议(protocol)扩展方法取代,Swift 2 的一个新特性,所以 enumerate() 现在是一个扩展方法对于 SequenceType:

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

并用作

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

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

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' is unavailable: call the 'enumerate()' method on the sequence,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31764548/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com