Looping with ForEach
Render rows from an array of data.
Looping with ForEach is a free SwiftUI Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SwiftUI Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why ForEach
Hardcoding rows works for two items, but real data lives in arrays. ForEach builds one row per element automatically. 🔁
let fruits = ["Apple", "Pear", "Kiwi"]ForEach Inside List
Drop a ForEach inside a List and it generates one row for every item in your collection, no matter how many there are.
List {
ForEach(fruits, id: \.self) { fruit in
Text(fruit)
}
}The Closure Gives You Each Item
The closure runs once per element, handing you that item so you can build its row from real data.
ForEach(fruits, id: \.self) { fruit in
Text(fruit)
}Identity Matters
SwiftUI needs to tell rows apart, so ForEach always wants a way to identify each element uniquely.
The id Parameter
For simple value arrays, pass id set to a key path like .self so each value can identify itself uniquely to SwiftUI.
ForEach(scores, id: \.self) { score in
Text("\(score)")
}Looping a Range
Need a fixed count instead of an array? Pass a range and ForEach hands you each index in turn, perfect for placeholder rows.
ForEach(0..<5) { index in
Text("Row \(index)")
}Building Rich Rows
Inside the closure you can return any view, so each ForEach row can be a full HStack with icons and labels.
ForEach(fruits, id: \.self) { fruit in
HStack {
Image(systemName: "leaf")
Text(fruit)
}
}Mixing Static and Dynamic
A List can hold a static header Text and a ForEach loop together, blending fixed and data-driven rows.
List {
Text("Fruits")
ForEach(fruits, id: \.self) { Text($0) }
}Looping Structs
When you loop an array of structs, each item carries multiple fields you can show across the row.
ForEach(people, id: \.name) { person in
Text(person.name)
}Data Drives the UI
Change the array and the rows follow right along. With ForEach, your list is simply a live mirror of whatever data you give it.
Beyond Lists
ForEach is not just for lists. It also fills VStack, HStack, and grids with repeated views from any collection.
VStack {
ForEach(fruits, id: \.self) { Text($0) }
}Quick Check
Recall what the ForEach closure receives.
Recap
You learned ForEach: it turns any collection into rows, handing you each item so your UI tracks your data automatically. 🎉
Frequently asked questions
Is the “Looping with ForEach” lesson free?
Yes — the full text of “Looping with ForEach” is free to read here on the web, and the SwiftUI Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SwiftUI Academy course, upgrade to CoddyKit PRO.
What will I learn in “Looping with ForEach”?
Render rows from an array of data. You practise SwiftUI 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 SwiftUI Academy?
No prior experience is required. SwiftUI Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Looping with ForEach” 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 SwiftUI Academy lesson?
Yes. Every SwiftUI 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
- Building a Static List
- Looping with ForEach
- Identifiable & Stable IDs
- Sections & List Styles