gpt4 book ai didi

ios - String 没有一个名为 characters 的成员?

转载 作者:搜寻专家 更新时间:2023-10-30 22:25:19 25 4
gpt4 key购买 nike

我正在尝试 Swift Playground 。当我尝试下面的代码时,它不起作用并告诉我 'String' 没有名为 '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 的一个属性。

你可能正在使用 Xcode 6.3.2 和 Swift 1.2,那么它就是

print("the number of characters in \(word) is \(count(word))")

Swift 2.0 改变了两件事:

  • String 不再符合SequenceType,你必须访问.characters 显式,
  • 全局 count() 函数已被“协议(protocol)扩展”方法 count() 取代。

关于ios - String 没有一个名为 characters 的成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30959515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com