# ForEach

## Overview

## Sample Codes

#### Example 1 - Show each attendee in a row view

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

#### Example 2

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

![](https://5317963-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F598GXhEFvy2PX7VpZjCw%2Fuploads%2FxTHyyPIlC0uV9eV8qr9D%2Fimage.png?alt=media\&token=847f603f-02e1-48b4-afa2-ce22b78824be)

#### Example 3

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

<figure><img src="https://5317963-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F598GXhEFvy2PX7VpZjCw%2Fuploads%2FB3BDuJvMVuwfkAeSqKhp%2Fimage.png?alt=media&#x26;token=9f187619-38af-45fd-91fd-7c894801d105" alt=""><figcaption></figcaption></figure>

## Sources

### Videos

{% embed url="<https://youtu.be/vSHbMhy6XH4>" %}
[Hacking with Swift / 100 Days / Day-57 - Why does \\.self work for ForEach?](https://www.hackingwithswift.com/books/ios-swiftui/why-does-self-work-for-foreach)
{% endembed %}

### Articles / Documents

* <https://medium.com/@ashokrawat086/when-to-use-swift-for-in-and-for-each-361d55d6cc45>
