0Pricing
SwiftUI Academy · Lesson

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

  1. Building a Static List
  2. Looping with ForEach
  3. Identifiable & Stable IDs
  4. Sections & List Styles
← Back to SwiftUI Academy