0Pricing
SwiftUI Academy · Lektion

Schleifen mit ForEach

Rendern Sie Zeilen aus einem Datenarray.

Schleifen mit ForEach ist eine kostenlose SwiftUI Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des SwiftUI Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der SwiftUI Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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. 🎉

Häufig gestellte Fragen

Ist die Lektion „Schleifen mit ForEach“ kostenlos?

Ja — der vollständige Text von „Schleifen mit ForEach“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des SwiftUI Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der SwiftUI Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Schleifen mit ForEach“?

Rendern Sie Zeilen aus einem Datenarray. Du übst SwiftUI Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um SwiftUI Academy zu starten?

Keine Vorkenntnisse erforderlich. SwiftUI Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.

Wie lange dauert die Lektion „Schleifen mit ForEach“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser SwiftUI Academy-Lektion Code schreiben und ausführen?

Ja. Jede SwiftUI Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Eine statische Liste erstellen
  2. Schleifen mit ForEach
  3. Identifiable und stabile IDs
  4. Abschnitte und Listenstile
← Zurück zu SwiftUI Academy