0Pricing
Swift Academy · Lesson

Counting and Iterating Characters

Iterate text correctly across emoji and accents.

Counting and Iterating Characters is a free Swift Academy lesson on CoddyKit — lesson 3 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.

Counting Characters

The count property of a String returns the number of Character values, which equals the number of user-perceived characters.

let word = "Swift"
print(word.count)

Count Reflects Visible Characters

Because Swift counts grapheme clusters, an accented letter or emoji counts as one, matching what a reader sees.

let cafe = "caf\u{e9}"
print(cafe.count)

Emoji Counting Correctness

A single emoji counts as one character, even though it spans multiple bytes and scalars internally.

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

Complex Emoji Still Count as One

Even multi-scalar emoji like a family or a flag count as a single Character, unlike naive byte or scalar counting.

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

Iterating with for-in

A for-in loop over a string visits each Character in order, correctly stepping over grapheme clusters.

for ch in "a\u{1F600}b" {
    print(ch)
}

Counting Specific Characters

Combine iteration with a counter, or use filter, to count occurrences of a particular character.

let text = "banana"
let aCount = text.filter { $0 == "a" }.count
print("a appears:", aCount)

Checking for Emptiness

Prefer isEmpty over comparing count to zero; it is clearer and can be faster.

let s = ""
print(s.isEmpty)
print("hello".isEmpty)

Indexing Is Not Integer-Based

Because characters vary in size, Swift strings use String.Index rather than integer subscripts.

let word = "Swift"
let first = word[word.startIndex]
print(first)

Iterating with Indices

You can iterate using indices when you need both the position and the character.

let word = "Hi"
for i in word.indices {
    print(word[i])
}

Enumerating with Offsets

Use enumerated() to get a running integer offset alongside each character.

for (i, ch) in "abc".enumerated() {
    print(i, ch)
}

Why Correct Counting Matters

Counting by grapheme clusters means length checks, truncation, and validation behave intuitively for international text and emoji.

let name = "Jose\u{301}"
print("Visible length:", name.count)

Quick Check

Recall how Swift counts a single emoji.

Recap: Counting and Iterating Characters

You learned that count returns the number of grapheme clusters so emoji and accents count as one, that for-in iterates characters correctly, that strings use String.Index not integers, and that enumerated() gives offsets.

let s = "ab\u{1F600}"
print("Count:", s.count)
for (i, ch) in s.enumerated() { print(i, ch) }

Frequently asked questions

Is the “Counting and Iterating Characters” lesson free?

Yes — the full text of “Counting and Iterating Characters” 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 “Counting and Iterating Characters”?

Iterate text correctly across emoji and accents. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Counting and Iterating Characters” 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