Chapter 61: Swift For Loop (Real-Life)

1. Classic real pattern #1 – Display a numbered list (most frequent use)

This is by far the most common reason people use for + enumerated() in everyday code.

Swift

Why this is so frequent:

  • Almost every app shows lists with numbers: tasks, search results, cart items, notifications, leaderboard positions, order history, chat messages…
  • Humans like 1-based numbering (1., 2., 3.), not 0-based
  • enumerated() gives you both index and item for free

2. Real pattern #2 – Shopping cart / order receipt

This pattern appears in almost every e-commerce or food delivery app.

Swift

Typical extensions in real apps:

  • cart.remove(at: index) when user taps remove
  • cart[index].quantity += 1 when user taps +
  • cart.contains { $0.name == “…” } to avoid duplicate items

3. Real pattern #3 – Progress / checklist / onboarding steps

Very common in apps that guide the user (onboarding, setup wizard, project checklist).

Swift

Real extension: In SwiftUI you would usually write:

Swift

4. Real pattern #4 – Build a numbered report / log

Very common in console tools, debug logs, export features.

Swift

5. Real pattern #5 – Simple numbered output without array

When you just need to repeat something N times with numbering.

Swift

6. Very Common Beginner Mistakes & How to Avoid Them

Mistake Wrong / Dangerous code Correct / Better habit Why?
Using 1…count instead of 0..<count for i in 1…array.count { print(array[i]) } for i in 0..<array.count { print(array[i]) } 1…count tries to access index = count → crash
Force-unwrapping first/last array.first! array.first ?? “None” or if let first = array.first Empty array → crash
Modifying array while iterating for item in array { array.append(item) } Build new array or use for index in array.indices Runtime crash: collection mutated
Forgetting enumerated() when numbering for item in array { print(“1. \(item)”) } for (i, item) in array.enumerated() { print(“\(i+1). \(item)”) } All items get numbered “1.”
Using for i in 0…array.count for i in 0…array.count { … } for i in 0..<array.count Crash on last index

7. Quick Summary – Which for style to choose

You want to do… Recommended style Why / When to prefer it
Just process each item (no position needed) for item in array { … } Simplest & most readable
Need position / numbering for (index, item) in array.enumerated() { … } Most common when showing numbered lists
Only need indices (rare) for index in array.indices { … } When modifying or using multiple parallel arrays
Repeat exact number of times for i in 0..<n { … } or for i in 1…n { … } Clear & safe
Want 1-based numbering in output for (index, item) in array.enumerated() { index + 1 } Natural for humans

8. Small Practice – Try these right now

  1. Create array of 6 favorite songs → Print them numbered (1. Song A, 2. Song B…)
  2. Create array of 8 numbers (any) → Print each number with its position and whether it is even or odd → Use enumerated()
  3. Create array of 5 tasks → Print “Task #1: …”, “Task #2: …” using index

Paste your code here if you want feedback, corrections or more polished versions!

What would you like to explore next?

  • forEach, map, filter, reduce (functional style)
  • Looping with indices and safe bounds checking
  • Nested for loops (2D grids, tables, patterns)
  • Sorting arrays (simple & custom)
  • Arrays in SwiftUI (List, ForEach, @State)
  • Or move to another topic (dictionaries, sets, optionals, switch…)

Just tell me — we’ll continue in the same clear, patient, detailed style 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *