繁体   English   中英

类型“字符串”不符合协议“序列”

[英]type “string” doesn't conform to protocol “sequence”

当我尝试打印字符串a中的每个字符时,为什么会出现这种情况?

import Foundation


let a = "what is this"
for b in a {
    print(b)
}

在此处输入图片说明

String不是序列,您需要调用characters属性以获取sequence

let a = "what is this"
for b in a.characters {
  print(b, terminator: "")
}

// "what is this"

如果要枚举所有字符,则必须使用Stringcharacters成员变量。

let a = "what is this"
for b in a.characters 
{
   print(b)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM