SwiftUI Lists & Forms ForEach
SwiftUI Lists & Forms: ForEach
Iterate over collections to generate views dynamically using ForEach.
ForEach
Render a list of items using ForEach.
Example
import SwiftUI
struct ForEachDemo: View {
let nums = [1,2,3]
var body: some View {
VStack {
ForEach(nums, id: \.self) { n in
Text("Item \(n)")
}
}
}
}
import SwiftUI
struct ContentView: View {
var body: some View { ForEachDemo() }
}
import SwiftUI
@main
struct MyApp: App {
var body: some Scene {
WindowGroup { ContentView() }
}
}
The example above shows a list of items using ForEach.