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)
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…