Looping with ForEach
Render rows from an array of data.
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)
}
}All lessons in this course
- Building a Static List
- Looping with ForEach
- Identifiable & Stable IDs
- Sections & List Styles