我正在尝试 Swift 游乐场。当我尝试下面的代码时,它不起作用,并告诉我'String' does not have a member named 'Characters'
。我希望打印the number of characters in cafe is 4
。你能给我一些提示吗?谢谢。
var word = "cafe"
print("the number of characters in \(word) is \(word.characters.count)")
最佳答案
characters
是Xcode 7 beta附带的"new" Swift 2中String
的属性。
您可能会在Swift 1.2中使用Xcode 6.3.2,然后
print("the number of characters in \(word) is \(count(word))")
这里的Swift 2.0改变了两件事:
String
不再符合SequenceType
,您必须访问.characters
显式,count()
函数已由“协议(protocol)扩展”方法count()
代替。 关于ios - 字符串没有名为character的成员?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30959515/