swiftui foreach iterate through array code example
Example 1: loop through array swift
let names = ["a", "b", "c"]
for name in names {
print(name)
}
Example 2: swiftui foreach
ForEach(1...5) { row in
Text("Row \(row)")
}
Example 3: foreach swiftui object
struct ContentView: View {
let colors: [Color] = [.red, .green, .blue]
var body: some View {
VStack {
ForEach(colors, id: \.self) { color in
Text(color.description.capitalized)
.padding()
.background(color)
}
}
}
}