通过字符串Swift 2.0进行迭代。

时间:2022-05-03 11:45:18

I am trying to do a very simple piece of code in Swift playgrounds.

我试图在Swift操场上做一段非常简单的代码。

var word = "Zebra"

for i in word {
  print(i)
}

However, I always get an error on line 3.

但是,我总是在第3行中得到一个错误。

'String' does not have a member named 'Generator'

'String'没有名为'Generator'的成员

Any ideas on why this doesn't work? Note: I am working in Xcode 7, with Swift 2.0 (Strings and Characters).

你知道为什么这行不通吗?注意:我使用的是Xcode 7,使用的是Swift 2.0(字符串和字符)。

4 个解决方案

#1


78  

As of Swift 2, String doesn't conform to SequenceType. However, you can use the characters property on String. characters returns a String.CharacterView which conforms to SequenceType and so can be iterated through with a for loop:

对于Swift 2,字符串不符合顺序类型。但是,您可以在字符串上使用characters属性。返回一个字符串。字符视图符合序列类型,因此可以使用for循环进行迭代:

let word = "Zebra"

for i in word.characters {
    print(i)
}

Alternatively, you could add an extension to String to make it conform to SequenceType:

或者,可以向字符串添加扩展名,使其符合SequenceType:

extension String: SequenceType {}

// Now you can use String in for loop again.
for i in "Zebra" {
    print(i)
}

Although, I'm sure Apple had a reason for removing String's conformance to SequenceType and so the first option seems like the better choice. It's interesting to explore what's possible though.

尽管如此,我确信苹果有理由删除String对SequenceType的一致性,因此第一个选项似乎是更好的选择。探索什么是可能的是很有趣的。

#2


9  

String doesn't conform to SequenceType anymore. However you can access the characters property of it this way:

字符串不再符合SequenceType。但是,您可以通过以下方式访问它的字符属性:

var word = "Zebra"

for i in word.characters {
    print(i)
}

Note that the documentation hasn't been updated yet.

注意,文档还没有更新。

#3


2  

Swift 3.0.1

斯威夫特3.0.1

Use the indices property of the characters property to access all of the indices of individual characters in a string.

使用characters属性的索引属性访问字符串中单个字符的所有索引。

let greeting = "Guten Tag!"
for index in greeting.characters.indices {
print("\(greeting[index]) ", terminator: "")
}
// Prints "G u t e n   T a g ! "

visit https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html

访问https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html

#4


2  

Swift 4

Forin loop:

Forin循环:

let word = "Swift 4"
for i in word {
    print(i)
}

map example:

地图的例子:

let word = "Swift 4"
_ = word.map({ print($0) })

forEach example:

为每一个例子:

let word = "Swift 4"
word.forEach({ print($0) })

#1


78  

As of Swift 2, String doesn't conform to SequenceType. However, you can use the characters property on String. characters returns a String.CharacterView which conforms to SequenceType and so can be iterated through with a for loop:

对于Swift 2,字符串不符合顺序类型。但是,您可以在字符串上使用characters属性。返回一个字符串。字符视图符合序列类型,因此可以使用for循环进行迭代:

let word = "Zebra"

for i in word.characters {
    print(i)
}

Alternatively, you could add an extension to String to make it conform to SequenceType:

或者,可以向字符串添加扩展名,使其符合SequenceType:

extension String: SequenceType {}

// Now you can use String in for loop again.
for i in "Zebra" {
    print(i)
}

Although, I'm sure Apple had a reason for removing String's conformance to SequenceType and so the first option seems like the better choice. It's interesting to explore what's possible though.

尽管如此,我确信苹果有理由删除String对SequenceType的一致性,因此第一个选项似乎是更好的选择。探索什么是可能的是很有趣的。

#2


9  

String doesn't conform to SequenceType anymore. However you can access the characters property of it this way:

字符串不再符合SequenceType。但是,您可以通过以下方式访问它的字符属性:

var word = "Zebra"

for i in word.characters {
    print(i)
}

Note that the documentation hasn't been updated yet.

注意,文档还没有更新。

#3


2  

Swift 3.0.1

斯威夫特3.0.1

Use the indices property of the characters property to access all of the indices of individual characters in a string.

使用characters属性的索引属性访问字符串中单个字符的所有索引。

let greeting = "Guten Tag!"
for index in greeting.characters.indices {
print("\(greeting[index]) ", terminator: "")
}
// Prints "G u t e n   T a g ! "

visit https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html

访问https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/StringsAndCharacters.html

#4


2  

Swift 4

Forin loop:

Forin循环:

let word = "Swift 4"
for i in word {
    print(i)
}

map example:

地图的例子:

let word = "Swift 4"
_ = word.map({ print($0) })

forEach example:

为每一个例子:

let word = "Swift 4"
word.forEach({ print($0) })