---
description: ╱🚧 under construction -> View Groupings
---

# ForEach

[SwiftUI](../..) ⟩ [view](..) ⟩ [groupings]() ⟩ ForEach &#x20;

{% hint style="success" %}
<mark style="color:purple;">`ForEach`</mark> generates [views](..) for <mark style="color:yellow;">each element</mark> in a [collection](../../../swift/collections).&#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](../../../swift/type/category/protocol/identifiable) 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](../view "mention")，也可能是其他東西，如：[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="💈" %}
* [examples](foreach/examples "mention") - simple examples.
* used by [binarybitpatternview](../../../swift/type/category/basic/numbers/binaryfloatingpoint/binarybitpatternview "mention") to list a [string](../../../swift/type/category/basic/string "mention")'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](../../lists/list)
* [scrollvgridforeach](../layout/grids/scrollvgridforeach "mention")
* [cannot-convert-value-of-type-self-to-expected-argument-type-binding-less-than-c-greater-than](../layout/cannot-convert-value-of-type-self-to-expected-argument-type-binding-less-than-c-greater-than "mention").
{% endtab %}

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

{% tab title="⬇️" %}
* [vgridforeach](../layout/grids/vgridforeach "mention")
* [scrollvgridforeach](../layout/grids/scrollvgridforeach "mention")
{% endtab %}

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