Blog

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. πŸ‘


Stay up-to-date with our newsletter or follow us on Twitter.

Back to the Blog

recent posts