1

I'm trying to pass data to TextField using combine. by creating a data model and using observableObject, but when I use it in textField it shows me the error. Cannot convert value of type 'String' to expected argument type 'Binding< String >'. I'm unable to understand it.

dataModel

struct People: Identifiable {
    var id = UUID()
    var name: String
    var amount: String
}

let peopleData = [
    People(name: "A",amount: ""),
    People(name: "B",amount: ""),
    People(name: "C",amount: "")
]

ObservableObject

import Combine
class PeopleAllData: ObservableObject{
    @Published var peopleStore: [People] = peopleData
}

TextField

@ObservedObject var store = PeopleAllData()

                    List{

                        ForEach(store.peopleStore){ item in
                        HStack {
                            TextField("person Name", text: item.name) //Error:- Cannot convert value of type 'String' to expected argument type 'Binding<String>'

                            Button(action: {}) {
                                Image(systemName: "minus.circle")
                                    .foregroundColor(.red)
                            }
                        }
                            
                        }
                        
                        
                        
                    }
                    .frame(width: screen.width, height: screen.height)
Atharv Sharma
  • 125
  • 2
  • 8
  • 1
    Does this answer your question? [@Binding and ForEach in SwiftUI](https://stackoverflow.com/questions/57340575/binding-and-foreach-in-swiftui) – New Dev Oct 22 '20 at 16:38

1 Answers1

1

You need Binding to array element via ObservedObject, like below

ForEach(store.peopleStore.indices, id: \.self){ i in
    HStack {
        TextField("person Name", text: $store.peopleStore[i].name)

Asperi
  • 228,894
  • 20
  • 464
  • 690