String-NSString Mismatch:
The NSString conception involves UTF-16 code‐ points. Each approach has its advantages. The NSString way makes for great speed and efficiency in comparison to Swift, which must walk the string to investigate how the characters are constructed; but the Swift way gives what you would intuitively think of as the right answer. To emphasize this difference, a nonliteral Swift string has no length property; its analog to an NSString’s length is the count of its utf16 property.
Fortunately, the element mismatch doesn’t arise very often in practice; but it can arise. Here’s a good test case:
let s = "Ha\u{030A}kon"
print(s.characters.count) // 5
let length = (s as NSString).length // or: s.utf16.count
print(length) // 6
We’ve created our string (the Norwegian name Håkon) using a Unicode code point that combines with the previous code point to form a character with a ring over it. Swift walks the whole string, so it normalizes the combination and reports five characters. Cocoa just sees at a glance that this string contains six 16-bit codepoints.