I am trying to use ForEach's iterator variable in a view that requires binding.
import SwiftUI
struct MyStruct: Identifiable {
public var id = UUID()
var name: String
var repetitions: Int
}
struct ContentView: View {
@State private var mystructs :[MyStruct] = [
MyStruct(name: "John", repetitions: 3),
MyStruct(name: "Mark", repetitions: 9)
]
var body: some View {
List {
ForEach (mystructs) { st in
VStack {
Text("\(st.name)")
TextField("Name", text: self.$mystructs[0].name)
TextField("Name", text: $st.name) // <- Got "Ambiguous reference..." error
}
}
}
}
}
The ForEach iterator works, as demonstrated by the Text view's use of st.name. And the first TextField demonstrates that binding to mystructs' element works. However, for the second TextField which is my real use case, causes the following compiler errors:
- Use of unresolved identifier $st
- Ambiguous reference to member of 'subscript'
Any thoughts?