Chapter 37: Swift Arrays: Loop

1. Why do we need different ways to loop?

When you have an array, you usually want to:

  • Do something with each item
  • Know the position (index) of each item
  • Transform items into a new array
  • Find something (first match, all matches…)
  • Sum, count, filter, or reduce values

Swift gives you several very elegant ways to do this — and choosing the right one makes your code clearer, safer, and more efficient.

2. The 5 most important ways to loop over arrays

Way 1 – Simple for-in loop (most common & most readable)

Swift

When to use this:

  • You only need the value (not the index)
  • You are doing something simple like printing, logging, calling a function, updating UI…

Real-life example – show list of tasks

Swift

Way 2 – for-in with index using enumerated()

This is the most common way when you need both the item and its position.

Swift

Output:

text

Real-life example – numbered list in console or debug log

Swift

Way 3 – Loop over indices only (indices property)

Use this when you mainly need the positions and will access the array manually.

Swift

When to use this style:

  • You need to modify the array while looping (carefully!)
  • You are working with multiple arrays at the same index
  • You want maximum control over iteration

Way 4 – forEach method (functional style)

Swift

Key differences from for-in:

  • Cannot use break or continue
  • Cannot return early from the function
  • Looks more “functional” (good when chaining with map, filter…)

Realistic use case – small logging or side effects

Swift

Way 5 – Modern for try or for await (when dealing with throwing or async items)

These are advanced but very common in real apps today.

Swift

(We’ll cover async loops later — just showing it exists.)

6. Very Common Real-Life Examples

Example 1 – Display numbered list in console or debug

Swift

Example 2 – Process items with position

Swift

Example 3 – Filter & transform with forEach (functional style)

Swift

7. Quick Summary – Which Loop Style to Choose

Situation Recommended style Why / when to prefer it
Just process each item (no index needed) for item in array { … } Simplest, most readable
Need the position (index) 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 arrays
Functional style, chaining, side effects array.forEach { … } Looks modern, good for small operations
Async / throwing items for try await …, for await … Modern concurrency code

8. Small Practice – Try these now

  1. Create array of 5 favorite foods → Print them numbered using enumerated() → Print only items that contain “a” in the name
  2. Create array of numbers 1…10 → Print each number and whether it is even or odd → Use both for-in and forEach styles
  3. Create array of 4 task names → Print “Task 1: …” up to “Task 4: …” using index

Paste your code if you want feedback or improvements!

What would you like to explore next about arrays?

  • Looping with filter, map, compactMap, reduce
  • Sorting arrays (simple & custom sorting)
  • Array slicing (prefix, suffix, dropFirst…)
  • Safe index access & bounds checking
  • Arrays in SwiftUI (List, ForEach, @State)
  • Or move to another topic (dictionaries, sets, optionals…)

Just tell me — we’ll keep going in the same clear, detailed, teacher-like style 😊

You may also like...

Leave a Reply

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