20

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

3 Answers 3

59

Oh, it is simple:

string.characters.count

2
  • 2
    You know what!! you posted the question on 9:03 AM and you got a brilliant vision at the time of 9:04 AM. Anyway still appreciate that ! Sep 21, 2016 at 12:55
  • @MohsinQureshi SO encourage to share your knowledge and post your question and answer same time. Jul 26, 2017 at 6:46
2

Again more simplified version

In Swift 4.0 and Above

let strLength = string.count
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.