0Pricing
Swift Academy · Lesson

Iteration patterns & mutability

Learn common iteration patterns over arrays/dictionaries/sets, use indices and enumerated(), and understand mutability with let vs var (copy-on-write basics).

Iteration patterns & mutability is a free Swift Academy lesson on CoddyKit — lesson 3 of 3. 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 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why iteration & mutability?

Goal: Iterate safely and clearly over collections, and know when they are mutable. We'll cover arrays, dictionaries, sets, indices, and enumerated(), plus let vs var.

for-in (Array)

for-in iterates directly over elements; simple and clear for arrays.

let nums = [10, 20, 30]
for n in nums {
    print("value:", n)
}

enumerated()

Use enumerated() to get (index, value) pairs during iteration.

let letters = ["a","b","c"]
for (i, ch) in letters.enumerated() {
    print(i, ch)   // 0 a, 1 b, 2 c
}

Dictionary & Set

Dictionaries yield (key, value) pairs. Sets are unordered—sorted() for stable output.

let ages = ["Ada": 20, "Bob": 25]
for (name, age) in ages {
    print(name, age)
}

let colors: Set<String> = ["red", "green", "blue"]
for c in colors.sorted() {       // sort for stable order
    print(c)
}

Indices & slices

Use startIndex, endIndex, and index(after:) for index-based access. Slices are ArraySlice views.

let arr = [5,6,7,8]
var i = arr.startIndex
while i < arr.endIndex {
    print("arr[", i, "] =", arr[i])
    i = arr.index(after: i)
}
let slice = arr[1...2]      // ArraySlice
print("slice", slice)

Mutability & COW

Mutability: declare with var to modify. Collections use copy-on-write—copies are cheap until mutated.

let fixed = [1,2,3]
// fixed.append(4) // error: cannot use mutating member on immutable value

var bag = [1,2,3]
bag.append(4)           // ok
var bagCopy = bag       // COW: cheap copy until either changes
bagCopy[0] = 9
print("bag", bag, "copy", bagCopy)

Mutability rule

Quick check: Which declaration lets you modify a collection?

Recap

Recap: Prefer element-based for-in, use enumerated() for index+value, sort Sets for order, navigate with String/Array indices, and mutate only with var (COW-aware).

Frequently asked questions

Is the “Iteration patterns & mutability” lesson free?

Yes — the full text of “Iteration patterns & mutability” is free to read here on the web, and the Swift Academy course includes 3 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 “Iteration patterns & mutability”?

Learn common iteration patterns over arrays/dictionaries/sets, use indices and enumerated(), and understand mutability with let vs var (copy-on-write basics). 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 3, so you can start here or from the beginning and move at your own pace.

How long does the “Iteration patterns & mutability” 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. Arrays, Dictionaries & Sets
  2. Sets — membership & operations
  3. Iteration patterns & mutability
← Back to Swift Academy