|
| 1 | +// |
| 2 | +// ForEach.swift |
| 3 | +// OpenSwiftUI |
| 4 | +// |
| 5 | +// Audited for RELEASE_2021 |
| 6 | +// Status: WIP |
| 7 | + |
| 8 | +/// A structure that computes views on demand from an underlying collection of |
| 9 | +/// identified data. |
| 10 | +/// |
| 11 | +/// Use `ForEach` to provide views based on a |
| 12 | +/// [RandomAccessCollection](https://developer.apple.com/documentation/swift/randomaccesscollection) |
| 13 | +/// of some data type. Either the collection's elements must conform to |
| 14 | +/// [Identifiable](https://developer.apple.com/documentation/swift/identifiable) or you |
| 15 | +/// need to provide an `id` parameter to the `ForEach` initializer. |
| 16 | +/// |
| 17 | +/// The following example creates a `NamedFont` type that conforms to |
| 18 | +/// [Identifiable](https://developer.apple.com/documentation/swift/identifiable), and an |
| 19 | +/// array of this type called `namedFonts`. A `ForEach` instance iterates |
| 20 | +/// over the array, producing new ``Text`` instances that display examples |
| 21 | +/// of each OpenSwiftUI ``Font`` style provided in the array. |
| 22 | +/// |
| 23 | +/// private struct NamedFont: Identifiable { |
| 24 | +/// let name: String |
| 25 | +/// let font: Font |
| 26 | +/// var id: String { name } |
| 27 | +/// } |
| 28 | +/// |
| 29 | +/// private let namedFonts: [NamedFont] = [ |
| 30 | +/// NamedFont(name: "Large Title", font: .largeTitle), |
| 31 | +/// NamedFont(name: "Title", font: .title), |
| 32 | +/// NamedFont(name: "Headline", font: .headline), |
| 33 | +/// NamedFont(name: "Body", font: .body), |
| 34 | +/// NamedFont(name: "Caption", font: .caption) |
| 35 | +/// ] |
| 36 | +/// |
| 37 | +/// var body: some View { |
| 38 | +/// ForEach(namedFonts) { namedFont in |
| 39 | +/// Text(namedFont.name) |
| 40 | +/// .font(namedFont.font) |
| 41 | +/// } |
| 42 | +/// } |
| 43 | +/// |
| 44 | +///  |
| 46 | +public struct ForEach<Data, ID, Content> where Data: RandomAccessCollection, ID: Hashable { |
| 47 | + |
| 48 | + /// The collection of underlying identified data that OpenSwiftUI uses to create |
| 49 | + /// views dynamically. |
| 50 | + public var data: Data |
| 51 | + |
| 52 | + /// A function to create content on demand using the underlying data. |
| 53 | + public var content: (Data.Element) -> Content |
| 54 | +} |
0 commit comments