I have an array of a model with just a single string with which I want to create instances of TextField. And I get an error for the TextField string binding. I know that is wrong. But how can fix it so that I can use textModel.name as a Binding?
import SwiftUI
struct ContentView: View {
@State var textModels = [TextModel]()
var body: some View {
HStack {
ForEach(textModels.indices, id: \.self) { index in
let textModel = textModels[index]
TextField("", text: textModel.name) // <----- Cannot convert value of type 'String' to expected argument type 'Binding<String>'
}
}.background(Color.green)
.onAppear {
textModels.append(TextModel(name: "Jim Thorton"))
textModels.append(TextModel(name: "Susan Murphy"))
textModels.append(TextModel(name: "Tom O'Donnell"))
textModels.append(TextModel(name: "Nancy Smith"))
}
}
}
struct TextModel: Hashable {
let name: String
}
Thanks.