Swift Academy · Lesson

Unicode Scalars and Code Points

Access the underlying scalar values of text.

Lesson 2 of 413 steps

Unicode Scalars and Code Points is a free Swift Academy lesson on CoddyKit. This is lesson 2 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.

Unicode Scalars

A Unicode scalar is a single code point in the Unicode standard, represented by Unicode.Scalar. Characters are built from one or more scalars.

let scalar: Unicode.Scalar = "A"
print(scalar)
print(scalar.value)

The value Property

Each scalar has a numeric .value: its Unicode code point as a number. The letter A is 65.

let a: Unicode.Scalar = "A"
let z: Unicode.Scalar = "Z"
print(a.value, z.value)

The unicodeScalars View

A string exposes its scalars through .unicodeScalars, a collection you can iterate.

for scalar in "Hi".unicodeScalars {
    print(scalar, scalar.value)
}

Scalars vs Characters

One Character may contain several scalars. An accented letter written with a combining mark has two scalars but one character.

let accented = "e\u{301}"
print("Characters:", accented.count)
print("Scalars:", accented.unicodeScalars.count)

Code Points from Hex

You can write a scalar by its hex code point using the backslash-u escape with braces.

let heart = "\u{2764}"
print(heart)
for s in heart.unicodeScalars { print(s.value) }

Creating Scalars from Numbers

You can construct a Unicode.Scalar from an integer code point; it is failable since not every number is valid.

if let s = Unicode.Scalar(65) {
    print(s)
}

Scalar Ranges

Because scalars have numeric values, you can compare and range over them, for example to test if a scalar is an ASCII digit.

let s: Unicode.Scalar = "7"
let isDigit = s.value >= 48 && s.value <= 57
print("Is digit:", isDigit)

ASCII Property

A scalar has an .isASCII property telling you whether it falls in the ASCII range.

let a: Unicode.Scalar = "a"
let emoji: Unicode.Scalar = "\u{1F600}"
print(a.isASCII, emoji.isASCII)

Scalar Properties

Scalars expose rich Unicode properties such as whether they are letters or whitespace via .properties.

let space: Unicode.Scalar = " "
print(space.properties.isWhitespace)

Mapping Characters to Scalars

You can break a Character into its component scalars to inspect how it is composed.

let c: Character = "e\u{301}"
for s in c.unicodeScalars {
    print(s.value)
}

Why Scalars Matter

Working at the scalar level is essential for low-level text processing, encoding, and understanding how characters are composed.

let flag = "\u{1F1F9}\u{1F1F7}"
print("Characters:", flag.count)
print("Scalars:", flag.unicodeScalars.count)

Quick Check

Recall the relationship between scalars and characters.

Recap: Unicode Scalars and Code Points

You learned that Unicode.Scalar is a single code point with a numeric .value, that strings expose a .unicodeScalars view, and that one Character may be composed of several scalars, which matters for low-level text work.

let text = "A\u{301}"
print("Characters:", text.count)
for s in text.unicodeScalars { print(s.value) }
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 “Unicode Scalars and Code Points” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Unicode Scalars and Code Points”, 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 “Unicode Scalars and Code Points”?

Access the underlying scalar values of text. 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 2 of 4.

How long does the “Unicode Scalars and Code Points” 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