Swift Strings: Unicode & Scalars
Swift Strings: Unicode & Scalars
Swift strings are Unicode-correct.
A single character can be composed of multiple Unicode scalars.
Visually identical strings can compare equal.
Composed Characters
Some characters are made from multiple Unicode scalars; Swift treats them as a single character for comparison and length.
Example
let e1 = "é"
let e2 = "e\u{301}" // e + COMBINING ACUTE ACCENT
print(e1 == e2)
print(e2)
This example shows that a precomposed character and a composed sequence are considered equal in Swift.
Unicode Scalars
Inspect the underlying Unicode scalar values with the unicodeScalars view.
The unicodeScalars view yields values of type UnicodeScalar.
Example
let s = "e\u{301}"
for scalar in s.unicodeScalars {
print(scalar.value) // 101, 769
}
This example prints the Unicode scalar code points that make up the character.