0Pricing
Swift Academy · Lesson

Encoding Views: utf8, utf16, unicodeScalars

Work with different encoded representations.

String Encoding Views

A Swift String offers several views onto the same text at different levels: utf8, utf16, and unicodeScalars.

let s = "Hi"
print(Array(s.utf8))
print(Array(s.utf16))

The utf8 View

The .utf8 view exposes the string as a sequence of UTF-8 code units (bytes). ASCII characters are one byte each.

let s = "AB"
for byte in s.utf8 {
    print(byte)
}

All lessons in this course

  1. Characters and Grapheme Clusters
  2. Unicode Scalars and Code Points
  3. Counting and Iterating Characters
  4. Encoding Views: utf8, utf16, unicodeScalars
← Back to Swift Academy