类型 "string" 不符合协议 "sequence"
type "string" doesn't conform to protocol "sequence"
当我尝试打印字符串 a 中的每个字符时,为什么会出现这种情况?
import Foundation
let a = "what is this"
for b in a {
print(b)
}
如果你想的话,你必须使用String
的characters
成员变量
枚举所有字符。
let a = "what is this"
for b in a.characters
{
print(b)
}
一个String
不是序列,需要调用characters
属性得到一个sequence
。
let a = "what is this"
for b in a.characters {
print(b, terminator: "")
}
// "what is this"
当我尝试打印字符串 a 中的每个字符时,为什么会出现这种情况?
import Foundation
let a = "what is this"
for b in a {
print(b)
}
如果你想的话,你必须使用String
的characters
成员变量
枚举所有字符。
let a = "what is this"
for b in a.characters
{
print(b)
}
一个String
不是序列,需要调用characters
属性得到一个sequence
。
let a = "what is this"
for b in a.characters {
print(b, terminator: "")
}
// "what is this"