<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/ForEach",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI7ForEachV"
  },
  "title" : "ForEach"
}
-->

# ForEach

A structure that computes views on demand from an underlying collection of
identified data.

```
struct ForEach<Data, ID, Content> where Data : RandomAccessCollection, ID : Hashable
```

## Overview

Use `ForEach` to provide views based on a
<doc://com.apple.documentation/documentation/Swift/RandomAccessCollection>
of some data type. Either the collection’s elements must conform to
<doc://com.apple.documentation/documentation/Swift/Identifiable> or you
need to provide an `id` parameter to the `ForEach` initializer.

The following example creates a `NamedFont` type that conforms to
<doc://com.apple.documentation/documentation/Swift/Identifiable>, and an
array of this type called `namedFonts`. A `ForEach` instance iterates
over the array, producing new [`Text`](/documentation/SwiftUI/Text) instances that display examples
of each SwiftUI [`Font`](/documentation/SwiftUI/Font) style provided in the array.

```
private struct NamedFont: Identifiable {
    let name: String
    let font: Font
    var id: String { name }
}

private let namedFonts: [NamedFont] = [
    NamedFont(name: "Large Title", font: .largeTitle),
    NamedFont(name: "Title", font: .title),
    NamedFont(name: "Headline", font: .headline),
    NamedFont(name: "Body", font: .body),
    NamedFont(name: "Caption", font: .caption)
]

var body: some View {
    ForEach(namedFonts) { namedFont in
        Text(namedFont.name)
            .font(namedFont.font)
    }
}
```

![A vertically arranged stack of labels showing various standard fonts,](images/com.apple.SwiftUI/SwiftUI-ForEach-fonts@2x.png)

Some containers like [`List`](/documentation/SwiftUI/List) or [`LazyVStack`](/documentation/SwiftUI/LazyVStack) will query the elements
within a for each lazily. To obtain maximal performance, ensure that
the view created from each element in the collection represents a
constant number of views.

For example, the following view uses an if statement which means each
element of the collection can represent either 1 or 0 views, a
non-constant number.

```
ForEach(namedFonts) { namedFont in
    if namedFont.name.count != 2 {
        Text(namedFont.name)
    }
}
```

You can make the above view represent a constant number of views by
wrapping the condition in a [`VStack`](/documentation/SwiftUI/VStack), an [`HStack`](/documentation/SwiftUI/HStack), or a [`ZStack`](/documentation/SwiftUI/ZStack).

```
ForEach(namedFonts) { namedFont in
    VStack {
        if namedFont.name.count != 2 {
            Text(namedFont.name)
        }
    }
}
```

When enabling the following launch argument, SwiftUI will log when
it encounters a view that produces a non-constant number of views
in these containers:

```
-LogForEachSlowPath YES
```

## Topics

### Creating a collection

[`init(_:)`](/documentation/SwiftUI/ForEach/init(_:))

Creates an instance that uniquely identifies and creates table rows
across updates based on the identity of the underlying data.

[`init(_:content:)`](/documentation/SwiftUI/ForEach/init(_:content:))

Creates an instance that uniquely identifies and creates map content
across updates based on the identity of the underlying data.

[`init(_:id:content:)`](/documentation/SwiftUI/ForEach/init(_:id:content:))

Creates an instance that uniquely identifies and creates map content
across updates based on the provided key path to the underlying data’s
identifier.

[`init(sections:content:)`](/documentation/SwiftUI/ForEach/init(sections:content:))

Creates an instance that uniquely identifies and creates views across
updates based on the sections of a given view.

[`init(subviews:content:)`](/documentation/SwiftUI/ForEach/init(subviews:content:))

Creates an instance that uniquely identifies and creates views across
updates based on the subviews of a given view.

### Creating an editable collection

[`init(_:editActions:content:)`](/documentation/SwiftUI/ForEach/init(_:editActions:content:))

Creates an instance that uniquely identifies and creates views across
updates based on the identity of the underlying data.

[`init(_:id:editActions:content:)`](/documentation/SwiftUI/ForEach/init(_:id:editActions:content:))

Creates an instance that uniquely identifies and creates views across
updates based on the identity of the underlying data.

### Accessing content

[`content`](/documentation/SwiftUI/ForEach/content)

A function to create content on demand using the underlying data.

[`data`](/documentation/SwiftUI/ForEach/data)

The collection of underlying identified data that SwiftUI uses to create
views dynamically.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)