Short answer

Use for item in array for every element, enumerated() when you also need a position, indices when you change elements in place, stride(from:to:by:) for steps or counting down, and zip to walk two arrays together (it stops at the shorter one). Prefer for-in over forEach when you may need break, and reach for first(where:), contains or map before writing a loop at all.

SWIFT TIPS · SWIFT

First posted on July 28, 2019, as "The Ultimate (FOR - LOOP) Swift". Rewritten on November 17, 2026. All code and output below was run on a Mac with Swift 6.2.3 (Xcode 26.2) on macOS 26.0.1, and the file was type-checked for an iOS 15 target in Swift 5 and Swift 6 modes. The short outlet-collection loop near the end was type-checked separately against the iOS 26.2 SDK with a stub outlet.

The 2019 post walked through six kinds of for loop: a plain loop over an array, a loop with an index from enumerated(), loops over ranges, forEach, zip, and zip combined with enumerated(), with a real use case for outlet collections. It promised a while loop post next.

Those are still the loops Swift developers use every day. This rewrite runs all of them, adds the cases where they behave differently than you'd expect, and keeps the examples small enough to paste into a Playground.

Every loop, run

let items = [1, 3, 10, 4, 6, 7, 14]

// 1. Every element
var sum = 0
for item in items { sum += item }
print("1 sum:", sum)

// 2. Index and element together
for (index, item) in items.enumerated() where item > 6 {
    print("2 index \(index) has \(item)")
}

// 3. Changing every element: map, or loop over indices
let ones = items.map { _ in 1 }
var copy = items
for i in copy.indices { copy[i] *= 2 }
print("3 map:", ones, "indices:", copy)

// 4. Ranges and steps
print("4 closed 1...5:", Array(1...5), "half-open 0..<5:", Array(0..<5))
print("4 stride by 3:", Array(stride(from: 0, to: 15, by: 3)), "countdown:", Array(stride(from: 5, through: 1, by: -1)))

// 5. Two arrays side by side; zip stops at the shorter one
let names = ["Ana", "Budi", "Citra"]
let scores = [90, 72]
for (name, score) in zip(names, scores) { print("5 \(name): \(score)") }

// 6. Index, element and a second array
let flags = [true, false, true]
for ((i, name), flag) in zip(names.enumerated(), flags) { print("6", i, name, flag) }

// 7. Stop early: break and continue work in for-in, not in forEach
for item in items {
    if item == 10 { print("7 found 10, stopping"); break }
}
var seen: [Int] = []
items.forEach { item in
    if item == 10 { return }       // skips this element only
    seen.append(item)
}
print("7 forEach with return kept going:", seen)

// 8. Search instead of looping
print("8 first > 6:", items.first(where: { $0 > 6 }) ?? -1, "contains 14:", items.contains(14))

// 9. Labeled break out of nested loops
outer: for row in 0..<3 {
    for col in 0..<3 where row * col == 2 {
        print("9 first product 2 at row \(row) col \(col)")
        break outer
    }
}

// 10. enumerated() offsets are not indices of an ArraySlice
let tail = items[3...]
for (offset, item) in tail.enumerated() where offset == 0 {
    print("10 offset \(offset) is index \(tail.startIndex), value \(item) == tail[\(tail.startIndex)] \(tail[tail.startIndex])")
}
print("10 tail.indices:", Array(tail.indices))

Output:

1 sum: 45
2 index 2 has 10
2 index 5 has 7
2 index 6 has 14
3 map: [1, 1, 1, 1, 1, 1, 1] indices: [2, 6, 20, 8, 12, 14, 28]
4 closed 1...5: [1, 2, 3, 4, 5] half-open 0..<5: [0, 1, 2, 3, 4]
4 stride by 3: [0, 3, 6, 9, 12] countdown: [5, 4, 3, 2, 1]
5 Ana: 90
5 Budi: 72
6 0 Ana true
6 1 Budi false
6 2 Citra true
7 found 10, stopping
7 forEach with return kept going: [1, 3, 4, 6, 7, 14]
8 first > 6: 10 contains 14: true
9 first product 2 at row 1 col 2
10 offset 0 is index 3, value 4 == tail[3] 4
10 tail.indices: [3, 4, 5, 6]

Notes on each one

1 and 2: element, then index and element

A plain for item in items is the right default. Add enumerated() only when you need a position, and add a where clause to skip elements without nesting an if. Line 2 printed only the items above 6, with their positions.

3: changing elements

The 2019 example set every element to 1 with for (key, num) in items.enumerated() { items[key] = 1 }. It works, but num is never used, and the compiler warns about that. If you're building a new array, map says it in one line. If you're changing an array in place, loop over indices, which line 3 used to double every value.

4: ranges and steps

1...5 includes 5, 0..<5 stops before it. For steps of more than one, or for counting down, use stride: stride(from: 0, to: 15, by: 3) printed 0 to 12, and stride(from: 5, through: 1, by: -1) counted from 5 to 1. A reversed range like 5...1 is not allowed and stops the program, so stride is the way down.

5 and 6: zip

zip pairs two sequences and stops at the shorter one. Line 5 shows it: three names and two scores printed only Ana and Budi, with no error. That's convenient when you expect equal lengths and a quiet bug when you don't, so check the counts first if a missing pair matters. Line 6 is the 2019 "key and zip" loop, which works the same way.

7: break and forEach

This is the difference people trip over. break stopped the for-in loop at 10. Inside forEach, break isn't allowed, and return only skips the current element. Line 7 shows the forEach carried on after 10 and collected everything except it. When you might stop early, use for-in.

8: don't loop at all

Many loops are searches in disguise. first(where:) found the first item above 6, and contains checked for 14, with no loop variable and no flag to manage.

9: leaving nested loops

A label like outer: lets break outer leave both loops at once, which line 9 used to stop at the first pair whose product is 2.

10: offsets aren't indices

enumerated() always counts from 0. For a slice like items[3...], the first offset was 0, but the slice's first index was 3. Using the offset as a subscript on the slice would read the wrong element, or crash. When you need real indices, loop over tail.indices or use zip(tail.indices, tail).

The outlet collection example

The 2019 post's best example was practical: three buttons in an @IBOutlet collection, each needing the same corner radius and title alignment. Instead of six near-identical lines, one loop styled them all. With the notes above, that loop reads best without indices at all:

for button in menuMidButtons {
    button.layer.cornerRadius = button.frame.height / 2
}

One caution from layout: a button's frame isn't final in viewDidLoad(), so set a corner radius that depends on height in viewDidLayoutSubviews(), or use a button configuration with a capsule corner style.

A quick check for your project

  1. Search for forEach blocks that try to stop early. Change them to for-in with break.
  2. Search for zip( on arrays that should be the same length, and check what happens when they aren't.
  3. Look for loops whose only job is to find or test one element. Replace them with first(where:) or contains(where:).

What the 2019 version said

Besides the examples above, the range section had a small slip: for item in 0..<9 { print(items) } printed the whole array nine times instead of the numbers, because it printed items rather than item. The comment next to it shows the intended output, 0 to 8.

Sources