Chapter 60: Swift For-Each Loop

1. What is forEach?

forEach is a method that belongs to all collection types (Array, Set, Dictionary, String, etc.).

It is a very short way to say:

“For every element in this collection, please run this piece of code once.”

Basic form:

Swift

Or even shorter (using implicit $0):

Swift

2. The most important differences compared to for-in

Aspect for item in array { … } array.forEach { … } Which one most developers prefer in 2025?
Syntax keyword for … in … method call .forEach { … } both — depends on context
Can use break / continue Yes No — cannot break or continue for-in when you need to break
Can return from the surrounding function Yes No — cannot return early for-in when you need early exit
Looks more functional / chainable No Yes — looks good with .map, .filter, etc. forEach when chaining
Reads like a sentence “For each item in array do…” “Array, please for each do…” personal taste
Performance basically same basically same no real difference
Mutating the collection while looping Allowed (but dangerous) Allowed (but dangerous) avoid both — use indices or new collection

Most important decision rule you will use every day:

  • If you need break, continue, or early return → use for-in
  • If you just want to do something simple with every item → forEach is often cleaner
  • If you are chaining operations → forEach fits better in functional style

3. Very first examples – feel the difference

Example A – Simple printing

Swift

All three give exactly the same output.

Example B – With index (very frequent pattern)

Swift

4. Real-life examples – code you will actually write

Example 1 – Logging / debugging (very common)

Swift

Example 2 – Update UI elements (very common in UIKit & SwiftUI)

Swift

Example 3 – Send analytics / log events

Swift

Example 4 – Process items with index (numbered output)

Swift

Example 5 – Apply action to every subview (UIKit classic)

Swift

5. Very Important Limitations of forEach

These are the reasons many experienced developers prefer for-in in some situations:

  1. You cannot use break or continue
Swift
  1. You cannot return early from the function
Swift

→ You cannot do this with forEach — it will always go through all elements.

  1. Harder to read when logic is complex
Swift

6. When to choose forEach vs for-in

Situation Prefer forEach? Prefer for-in? Reason / guideline
Very simple action per item Yes Clean & short
Chaining with map/filter/reduce Yes Looks more functional
Just fire-and-forget (logging, analytics, UI update) Yes No need for control flow
Need break, continue or early return Yes for-in allows control flow
Complex logic inside the loop Yes Easier to read & debug
Need index and want numbered output Yes (with enumerated()) Yes Both work well — personal taste

7. Quick Summary – forEach cheat sheet

Swift

8. Small Practice – Try these

  1. Create array of 5 favorite foods → Use forEach to print “I like to eat …”
  2. Create array of numbers 1…10 → Use forEach + enumerated() to print “Number 1 is odd”, “Number 2 is even”…
  3. Create array of 6 task names → Use forEach to print “Task 1: …”, “Task 2: …” (use enumerated())

Paste your code here if you want feedback or want to see even cleaner versions!

What would you like to explore next?

  • forEach vs for-in vs map vs filter — decision guide
  • Looping with indices and safe bounds checking
  • Nested loops (for inside for)
  • Sorting arrays
  • 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 *