Other representations

Depending on what we need to do with String objects in our code, we may need to access other representations of the string it contains. One frequent case is interfacing with ASCII strings, in either UTF 8 or UTF 16 encodings.

We will look at these four representations:

  • UTF-8, as frequently used in database columns
  • UTF-16, which is the basis of NSString objects
  • UTF-32, the format of so-called Unicode code points
  • Unicode scalars

There are two String methods, utf8 and utf16, which return strings in the required formats:

let answer = "Enjoy the trip " answer.utf16.count    // 18 answer.utf8.count    // 20 

We see here that the ...

Get Mastering macOS Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.