ForEach

#foreach #self

Overview

Sample Codes

Example 1 - Show each attendee in a row view

Section(header: Text("Attendees")) {
    ForEach(data.attendees) { attendee in
        Text(attendee.name)

Example 2

struct ContentView: View {
    var body: some View {
        NavigationView {
            Form {
                ForEach(0..<11) {number in
                    Text("Row \(number)")
                }
            }
            .navigationTitle("SwiftUI")
            .navigationBarTitleDisplayMode(.inline)
        }
    }
}

Example 3

List {
    ForEach([2, 4, 6, 8, 10], id: \.self) {
        Text("\($0) is even")
    }
}

Sources

Videos

Hacking with Swift / 100 Days / Day-57 - Why does \.self work for ForEach?

Articles / Documents

Last updated