我试图循环遍历String获取字符及其索引。听起来很简单..但我没有找到正确的语法。
以下是我的尝试:
let str = "testString"
for (index, char) in str.characters {
print(index)
print(char)
}
或
for (index, char) in enumerate(str.characters) {...}
答案 0 :(得分:9)
let str = "testString"
for (index, element) in str.characters.enumerate() {
print(index)
print(element)
}
答案 1 :(得分:0)
Swift 4
let str = "Swift 4"
for (index, element) in str.enumerated() {
print(index)
print(element)
}