Swift Tip: Unicode Scalar Properties
As part of the ongoing rewrite of Advanced Swift, this week we turn our attention to the chapter on Strings.
One of our favorite new additions to Swift's Strings API is the Unicode.Scalar.Properties
type.
Before Swift 5, we had to use Foundation's CharacterSet
to check certain Unicode properties. For example, to know whether something is a newline or a space, you could do CharacterSet.whitespacesAndNewlines.contains(c)
. While the name is CharacterSet
, it really should have been called UnicodeScalarSet
in Swift, because it only works on unicode scalars.
In Swift 5, we now have a built-in way to check Unicode properties. For instance, we can check if a name starts with an uppercase letter:
for name in ["Alice", "bob", "ฤรกbel"] {
print(name.unicodeScalars.first!.properties.isUppercase)
}
// Prints true, false, true
We can also query other kinds of properties:
print(String("1+โซ".unicodeScalars.filter { $0.properties.isMath }))
// Prints +โซ
It's also possible to get a scalar's official Unicode name:
print("๐จโ๐ฉโ๐งโ๐ง\nโ".unicodeScalars.map { $0.properties.name })
This prints the following (note that the ๐จโ๐ฉโ๐งโ๐ง emoji consist of 7 scalars):
[Optional("MAN"), Optional("ZERO WIDTH JOINER"),
Optional("WOMAN"), Optional("ZERO WIDTH JOINER"),
Optional("GIRL"), Optional("ZERO WIDTH JOINER"), Optional("GIRL"),
nil, // The newline
Optional("PLACE OF INTEREST SIGN")]
This new API feels much more native to Swift (previously, you would achieve the same using Foundation's StringTransform
), which is really nice to see.
The new edition of Advanced Swift should be released at the beginning of May. The update will be free for all existing Ebook readers. ๐