TextField Binding

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.

You should try $textModel.name.

Well, I guess I could do the following.

import SwiftUI

struct ContentView: View {
	@State private var names: [String] = ["Jim Thorton", "Susan Murphy", "Tom O'Donnell", "Nancy Smith"]
	
	var body: some View {
		HStack {
			ForEach($names, id: \.self) { $name in
				TextField("", text: $name)
					.fixedSize()
			}
		}
	}
}

When you say:

let textModel = textModels[index]

...you are creating a temporary "textModel", which only lives for 1 pass of your ForEach.
So you can't use it as a Binding (because it won't exist, when you try and access it later).

(I'm away from Xcode, but) I would omit the line above, and try:

TextField("", text: $textModels[index].name)
TextField Binding
 
 
Q