简体   繁体   中英

How can I get the length of a String in Swift 2.0?

So far it was:

let string = "my example string"
if count(string) >= 3 { ... }

But now I get an error:

count is unavailable: access the count property on the collection. Type String doesn't conform to protocol CollectionType

哦,很简单:

string.characters.count

Again more simplified version

In Swift 4.0 and Above

let strLength = string.count

It should be like that

let string = "my example string"
let length = string.characters.count

Because in Swift 2.0 Apple changed global functions to protocol extensions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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