> For the complete documentation index, see [llms.txt](https://lochiwei.gitbook.io/ios/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lochiwei.gitbook.io/ios/swiftui/view/grouping/foreach.md).

# ForEach

╱🚧 under construction -> View Groupings

[SwiftUI](/ios/swiftui.md) ⟩ [view](/ios/swiftui/view.md) ⟩ [groupings](/ios/swiftui/view/grouping.md) ⟩ ForEach &#x20;

{% hint style="success" %} <mark style="color:purple;">`ForEach`</mark> generates [views](/ios/swiftui/view.md) for <mark style="color:yellow;">each element</mark> in a [collection](/ios/swift/collections.md).&#x20;

<mark style="color:purple;">`ForEach`</mark> requires that each element provide an <mark style="color:yellow;">`id`</mark>,&#x20;

* either <mark style="color:yellow;">explicitly</mark> in the <mark style="color:purple;">`ForEach`</mark> initializer,&#x20;
* or by conforming to the [Identifiable](/ios/swift/type/category/protocol/identifiable.md) protocol.

```swift
// <collection> must conform to Identifiable
ForEach(collection) { element in
    Text(element.name)
}
```

{% endhint %}

{% hint style="danger" %}
⭐️ 注意：

* **ForEach** <mark style="color:red;">**doesn’t**</mark>**&#x20;get hit** by the <mark style="color:red;">**10-view limit**</mark>.  ⭐️
* <mark style="color:purple;">**ForEach**</mark> 不一定是 [View](/ios/swiftui/view/view.md)，也可能是其他東西，如：[TableRowContent](https://developer.apple.com/documentation/swiftui/tablerowcontent#relationships)

(👉 See: 📜 協定)
{% endhint %}

{% tabs %}
{% tab title="📘" %}

* [SwiftUI](https://developer.apple.com/documentation/swiftui) ⟩ [View groupings](https://developer.apple.com/documentation/swiftui/view-groupings) ⟩ [ForEach](https://developer.apple.com/documentation/swiftui/foreach)
* Swift ⟩  [Basic Behaviors](https://developer.apple.com/documentation/swift/swift_standard_library/basic_behaviors) ⟩ [Identifiable](https://developer.apple.com/documentation/swift/identifiable)
  {% endtab %}

{% tab title="📗" %}

* [x] Hacking with Swift - [Creating views in a loop](https://www.hackingwithswift.com/books/ios-swiftui/creating-views-in-a-loop)
* [ ] Sarun ⟩
  * [x] [Create a list of views in SwiftUI using ForEach](https://sarunw.com/posts/create-list-of-views-in-swiftui-using-foreach/)
  * [ ] [For loop in SwiftUI using ForEach](https://sarunw.com/posts/swiftui-foreach/)
  * [ ] [What is the difference between List and ForEach in SwiftUI](https://sarunw.com/posts/difference-between-list-foreach/)
    {% endtab %}

{% tab title="💈" %}

* [ForEach examples](/ios/swiftui/view/grouping/foreach/examples.md) - simple examples.
* used by [BinaryBitPatternView](/ios/swift/type/category/basic/numbers/binaryfloatingpoint/binarybitpatternview.md) to list a [String](/ios/swift/type/category/basic/string.md)'s characters.
  {% endtab %}

{% tab title="📜" %}

* [`AccessibilityRotorContent`](https://developer.apple.com/documentation/swiftui/accessibilityrotorcontent)\
  Conforms when `Data` conforms to `RandomAccessCollection`, `ID` conforms to `Hashable`, and `Content` conforms to `AccessibilityRotorContent`.
* [`DynamicTableRowContent`](https://developer.apple.com/documentation/swiftui/dynamictablerowcontent)\
  Conforms when `Data` conforms to `RandomAccessCollection`, `ID` conforms to `Hashable`, and `Content` conforms to `TableRowContent`.
* [`DynamicViewContent`](https://developer.apple.com/documentation/swiftui/dynamicviewcontent)\
  Conforms when `Data` conforms to `RandomAccessCollection`, `ID` conforms to `Hashable`, and `Content` conforms to `View`.
* [`TableRowContent`](https://developer.apple.com/documentation/swiftui/tablerowcontent)\
  Conforms when `Data` conforms to `RandomAccessCollection`, `ID` conforms to `Hashable`, and `Content` conforms to `TableRowContent`.
* [`View`](https://developer.apple.com/documentation/swiftui/view) ⭐️\
  Conforms when `Data` conforms to `RandomAccessCollection`, `ID` conforms to `Hashable`, and `Content` conforms to `View`.
  {% endtab %}

{% tab title="👥" %}

* [🔲 List](/ios/swiftui/lists/list.md)
* [ScrollVGridForEach](/ios/swiftui/view/layout/grids/scrollvgridforeach.md)
* [Cannot convert value of type 'Self' to expected argument type 'Binding\<C>'](/ios/swiftui/view/layout/cannot-convert-value-of-type-self-to-expected-argument-type-binding-less-than-c-greater-than.md).
  {% endtab %}

{% tab title="🗣" %}

* [What is the difference between List and ForEach in SwiftUI?](https://stackoverflow.com/a/56536206/5409815)  ⭐️
  {% endtab %}

{% tab title="⬇️" %}

* [VGridForEach](/ios/swiftui/view/layout/grids/vgridforeach.md)
* [ScrollVGridForEach](/ios/swiftui/view/layout/grids/scrollvgridforeach.md)
  {% endtab %}

{% tab title="🖥️" %}
{% embed url="<https://youtu.be/eDCBFXpmOkk>" %}
tundsdev ⟩ How To Use ForEach In SwiftUI With Identifiable
{% endembed %}
{% endtab %}
{% endtabs %}
