Skip to main content

Measure strings using their character count

About 1 minSwiftArticle(s)bloghackingwithswift.comswiftswift-2.0

Measure strings using their character count 관련

HACKING WITH SWIFT

What's new in Swift?

Measure strings using their character count | Changes in Swift 2.0

Measure strings using their character count

Available from Swift 2.0

In Swift 2.2 the way strings are measured changed yet again. What was countElements() became count() in Swift 1.1, and in Swift 2.0 was removed entirely.

Instead, you should access the characters property of your String, then call count on that, like this:

let string = "Hello, Swift!"
let count = string.characters.count
print(count)

Note

This has changed in later versions of Swift – you should access the count property of strings directly.

Other changes in Swift 2.0…
Throwing errors | Changes in Swift 2.0

Throwing errors
Use the guard keyword for early returns | Changes in Swift 2.0

Use the guard keyword for early returns
Use the defer keyword to delay work until your scope exits | Changes in Swift 2.0

Use the defer keyword to delay work until your scope exits
Mutability warnings | Changes in Swift 2.0

Mutability warnings
Checking API availability | Changes in Swift 2.0

Checking API availability

이찬희 (MarkiiimarK)
Never Stop Learning.