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

Lesson 3 of 38 steps

Iteration patterns & mutability is a free Swift Academy lesson on CoddyKit. This is lesson 3 of 3. 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 3 lessons in total.

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

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 “Iteration patterns & mutability” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Iteration patterns & mutability”, 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 3 lessons in total.

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

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