# ForEach

[SwiftUI](https://lochiwei.gitbook.io/ios/swiftui) ⟩ [view](https://lochiwei.gitbook.io/ios/swiftui/view) ⟩ [groupings](https://lochiwei.gitbook.io/ios/swiftui/view/grouping) ⟩ ForEach &#x20;

{% hint style="success" %} <mark style="color:purple;">`ForEach`</mark> generates [views](https://lochiwei.gitbook.io/ios/swiftui/view) for <mark style="color:yellow;">each element</mark> in a [collection](https://lochiwei.gitbook.io/ios/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](https://lochiwei.gitbook.io/ios/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](https://lochiwei.gitbook.io/ios/swiftui/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](https://lochiwei.gitbook.io/ios/swiftui/view/grouping/foreach/examples "mention") - simple examples.
* used by [binarybitpatternview](https://lochiwei.gitbook.io/ios/swift/type/category/basic/numbers/binaryfloatingpoint/binarybitpatternview "mention") to list a [string](https://lochiwei.gitbook.io/ios/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](https://lochiwei.gitbook.io/ios/swiftui/lists/list)
* [scrollvgridforeach](https://lochiwei.gitbook.io/ios/swiftui/view/layout/grids/scrollvgridforeach "mention")
* [cannot-convert-value-of-type-self-to-expected-argument-type-binding-less-than-c-greater-than](https://lochiwei.gitbook.io/ios/swiftui/view/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](https://lochiwei.gitbook.io/ios/swiftui/view/layout/grids/vgridforeach "mention")
* [scrollvgridforeach](https://lochiwei.gitbook.io/ios/swiftui/view/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 %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/ios/swiftui/view/grouping/foreach.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
