// // ForEach.swift // TUIKit // // Iteration over data collections for view generation. // /// A view that generates views from a collection of data. /// /// `ForEach` iterates over a collection and creates a view for each /// element. The collection elements must be `Identifiable` or an /// explicit ID key path must be provided. /// /// # Example with Identifiable /// /// ```swift /// struct Item: Identifiable { /// let id: String /// let name: String /// } /// /// let items = [Item(id: "1", name: "One"), Item(id: "2", name: "Two")] /// /// VStack { /// ForEach(items) { item in /// Text(item.name) /// } /// } /// ``` /// /// # Example with explicit ID key path /// /// ```swift /// let names = ["Anna", "Bob", "Clara"] /// /// VStack { /// ForEach(names, id: \.self) { name in /// Text(name) /// } /// } /// ``` public struct ForEach: View { /// The underlying data collection. public let data: Data /// The key path to the unique ID of each element. public let idKeyPath: KeyPath /// The closure that creates a view for each element. public let content: (Data.Element) -> Content /// Creates a ForEach with an explicit ID key path. /// /// - Parameters: /// - data: The collection to iterate over. /// - id: The key path to the unique ID of each element. /// - content: The closure that creates the view for each element. public init( _ data: Data, id: KeyPath, @ViewBuilder content: @escaping (Data.Element) -> Content ) { self.data = data self.idKeyPath = id self.content = content } public var body: Never { fatalError("ForEach renders its children directly") } } // MARK: - ForEach with Identifiable extension ForEach where Data.Element: Identifiable, ID == Data.Element.ID { /// Creates a ForEach for Identifiable elements. /// /// - Parameters: /// - data: The collection with Identifiable elements. /// - content: The closure that creates the view for each element. public init( _ data: Data, @ViewBuilder content: @escaping (Data.Element) -> Content ) { self.data = data self.idKeyPath = \Data.Element.id self.content = content } } // MARK: - ForEach with Range extension ForEach where Data == Range, ID == Int { /// Creates a ForEach over an integer range. /// /// - Parameters: /// - data: The range, e.g., `0..<10`. /// - content: The closure that creates the view for each index. public init( _ data: Range, @ViewBuilder content: @escaping (Int) -> Content ) { self.data = data self.idKeyPath = \.self self.content = content } }