Structure

ForEach

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

Declaration 宣言

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

Overview 概要

Use ForEach to provide views based on a RandomAccessCollection of some data type. Either the collection’s elements must conform to Identifiable or you need to provide an id parameter to the ForEach initializer.

The following example creates a NamedFont type that conforms to 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,

Topics 話題

Creating a Collection from a Range

Creating a Collection from Data

Generating Rotor Content

Creating a Collection of Table Rows

Accessing Content 内容にアクセスする

Relationships 関係

Conforms To 次に準拠

  • AccessibilityRotorContent
    Conforms when Data conforms to RandomAccessCollection, ID conforms to Hashable, and Content conforms to AccessibilityRotorContent.
  • DynamicTableRowContent
    Conforms when Data conforms to RandomAccessCollection, ID conforms to Hashable, and Content conforms to TableRowContent.
  • DynamicViewContent
    Conforms when Data conforms to RandomAccessCollection, ID conforms to Hashable, and Content conforms to View. DataRandomAccessCollectionに準拠する、IDHashableに準拠する、そしてContentViewに準拠する時に準拠します。
  • TableRowContent
    Conforms when Data conforms to RandomAccessCollection, ID conforms to Hashable, and Content conforms to TableRowContent.
  • View
    Conforms when Data conforms to RandomAccessCollection, ID conforms to Hashable, and Content conforms to View. DataRandomAccessCollectionに準拠する、IDHashableに準拠する、そしてContentViewに準拠する時に準拠します。

See Also 参照

Lists