---
title: "init(_:id:content:)"
framework: swiftui
role: symbol
role_heading: Initializer
path: "swiftui/foreach/init(_:id:content:)"
---

# 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.

## Declaration

```swift
@MainActor init(_ data: Data, id: KeyPath<Data.Element, ID>, @MapContentBuilder content: @escaping (Data.Element) -> Content)
```

## Parameters

- `data`: The data that the doc://com.apple.SwiftUI/documentation/SwiftUI/ForEach instance uses to create map content dynamically.
- `id`: The key path to the provided data’s identifier.
- `content`: The map content builder that creates map content dynamically.

## Mentioned in

Creating performant scrollable stacks

## Discussion

Discussion It’s important that the id of a data element doesn’t change, unless the data element has been replaced with a new data element that has a new identity. If the id of a data element changes, then the map content generated from that data element will lose any current state and animations.

## See Also

### Creating a collection

- [init(_:)](swiftui/foreach/init(_:).md)
- [init(_:content:)](swiftui/foreach/init(_:content:).md)
- [init(sections:content:)](swiftui/foreach/init(sections:content:).md)
- [init(subviews:content:)](swiftui/foreach/init(subviews:content:).md)
