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