One Logo Pool Ball
SwiftUICore · IOS API REFERENCE

ForEach

struct · iOS 13.0+ · SDK 27.1

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

A structure that computes views on demand from an underlying collection of identified data. 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`` instances that display examples of each 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, such as Large Title and Headline.](SwiftUI-ForEach-fonts.png) Some containers like ``List`` or ``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``, an ``HStack``, or a ``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

Members (0)

This type has no directly callable members in this snapshot. See the framework’s skipped declarations for unsupported APIs.

Skipped declarations

9 declarations were excluded from these bindings. Each reason comes from the generator or the iOS SDK compiler.

ForEach.init(sections:content:)

callback cannot propagate synchronous failure

ForEach.init(subviews:content:)

callback cannot propagate synchronous failure

ForEach.data

This declaration needs Swift generic type arguments. (no canonical witness for Data)

ForEach.content

This declaration needs Swift generic type arguments. (no canonical witness for Data.Element)

ForEach.init(_:content:)

This declaration needs Swift generic type arguments. (no canonical witness for Data)

ForEach.init(_:id:content:)

This declaration needs Swift generic type arguments. (no canonical witness for Data)

ForEach.init(_:content:)

This declaration needs Swift generic type arguments. (no canonical witness for C)

ForEach.init(_:id:content:)

This declaration needs Swift generic type arguments. (no canonical witness for C)

ForEach.init(_:content:)

callback cannot propagate synchronous failure

All SwiftUICore types →