0Pricing
Swift Academy · Lesson

Encoding Views: utf8, utf16, unicodeScalars

Work with different encoded representations.

Encoding Views: utf8, utf16, unicodeScalars is a free Swift Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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)
}

Multi-Byte UTF-8

Non-ASCII characters take multiple UTF-8 bytes. An accented letter or emoji expands to several code units.

let s = "\u{e9}"
print("UTF-8 bytes:", Array(s.utf8))

The utf16 View

The .utf16 view shows UTF-16 code units. Most common characters are one unit; characters above the basic plane use two (a surrogate pair).

let s = "A\u{1F600}"
print("UTF-16 units:", Array(s.utf16))

The unicodeScalars View

The .unicodeScalars view exposes the underlying code points, sitting between characters and raw bytes.

let s = "Hi\u{1F600}"
for scalar in s.unicodeScalars {
    print(scalar.value)
}

Comparing the Views

The same string yields different counts in each view because encodings represent characters with different numbers of units.

let s = "\u{1F600}"
print("characters:", s.count)
print("scalars:", s.unicodeScalars.count)
print("utf16:", s.utf16.count)
print("utf8:", s.utf8.count)

Why UTF-8

UTF-8 is the dominant encoding for files and networks. The .utf8 view lets you produce exactly those bytes.

let message = "OK"
let bytes = Array(message.utf8)
print(bytes)

Why UTF-16

UTF-16 is used by some platforms and APIs. The .utf16 view interoperates with them.

let s = "Hi"
print("UTF-16 count:", s.utf16.count)

Reconstructing a String

You can rebuild a string from UTF-8 bytes using String(decoding:as:), completing a round trip.

let bytes: [UInt8] = Array("Swift".utf8)
let restored = String(decoding: bytes, as: UTF8.self)
print(restored)

Counting Bytes vs Characters

Byte length differs from character length for non-ASCII text. Choose the view that matches your task: display vs storage.

let s = "caf\u{e9}"
print("Characters:", s.count)
print("UTF-8 bytes:", s.utf8.count)

Choosing the Right View

Use characters for display logic, scalars for Unicode analysis, and utf8 or utf16 for encoding, storage, and interoperation.

let s = "A\u{e9}\u{1F600}"
print("chars:", s.count, "scalars:", s.unicodeScalars.count, "utf8:", s.utf8.count)

Quick Check

Recall what the utf8 view contains.

Recap: Encoding Views

You learned that a String offers utf8, utf16, and unicodeScalars views over the same text, that their counts differ because encodings use different unit sizes, and that you pick a view based on whether you need display, analysis, or encoding for storage and networking.

let s = "Hi\u{1F600}"
print("chars:", s.count, "utf8:", s.utf8.count, "utf16:", s.utf16.count)

Frequently asked questions

Is the “Encoding Views: utf8, utf16, unicodeScalars” lesson free?

Yes — the full text of “Encoding Views: utf8, utf16, unicodeScalars” is free to read here on the web, and the Swift Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Swift Academy course, upgrade to CoddyKit PRO.

What will I learn in “Encoding Views: utf8, utf16, unicodeScalars”?

Work with different encoded representations. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Encoding Views: utf8, utf16, unicodeScalars” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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