Code that reproduces the issue
import SwiftUI
@main
struct TextFieldsGridApp: App {
@State private var controller = Controller()
var body: some Scene {
WindowGroup {
GridView()
.environment(controller)
}
}
}
struct GridView: View {
@Environment(Controller.self) private var c
var body: some View {
VStack {
ForEach(0..<c.strings.count, id: \.self) { r in
HStack {
ForEach(0..<4, id: \.self) { c in
TextField(
"",
text: c.strings[r][c]
)
.textFieldStyle(.roundedBorder)
}
}
}
}
.padding()
}
}
#Preview {
GridView()
.environment(Controller())
}
@Observable
class Controller {
private(set) var strings: [[String]] = Array(
repeating: Array(repeating: "A", count: 4),
count: 4,
)
}
Error
Cannot convert value of type 'Range<Int>' to expected argument type 'Binding<C>', caused by ForEach(0..<4, id: \.self) { c in.
Which I do not get if I say, for example:
struct GridView: View {
@State private var text = "H"
// ...
TextField("", text: $text)
Environment
MacBook Air M1 8GB iOS Sequoia 15.5 Xcode 16.4 Preview: iPhone 16 Pro, iOS 18.5.