Swift Academy · Lesson

Encoding Views: utf8, utf16, unicodeScalars

Work with different encoded representations.

Lesson 4 of 413 steps

Encoding Views: utf8, utf16, unicodeScalars is a free Swift Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

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)
Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

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

Yes — any 3 lessons of the Swift Academy learning path, including “Encoding Views: utf8, utf16, unicodeScalars”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

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