如何在 Swift 2.0 中获取字符串的长度?
How can I get the length of a String in Swift 2.0?
到目前为止是:
let string = "my example string"
if count(string) >= 3 { ... }
但现在我得到一个错误:
count
is unavailable: access the count
property on the collection. Type String
doesn't conform to protocol CollectionType
哦,很简单:
string.characters.count
再次简化版本
在 Swift 4.0 及更高版本中
let strLength = string.count
应该是这样
let string = "my example string"
let length = string.characters.count
因为在 Swift 2.0 Apple 中将全局函数更改为协议扩展。
到目前为止是:
let string = "my example string"
if count(string) >= 3 { ... }
但现在我得到一个错误:
count
is unavailable: access thecount
property on the collection. TypeString
doesn't conform to protocolCollectionType
哦,很简单:
string.characters.count
再次简化版本
在 Swift 4.0 及更高版本中
let strLength = string.count
应该是这样
let string = "my example string"
let length = string.characters.count
因为在 Swift 2.0 Apple 中将全局函数更改为协议扩展。