Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SwiftUI Setting Bind<String> in Text field

I’ve got the following object that contain list of strings:

class handler: ObservableObject {
  @Published var items = [String]()
}

In some closure, I set this list with valid values :

for item in item_list! {
  self.handler.items.append(item.stringData)
}

and in the ContentView part I’ve got Picker that support to present that list of strings in realtime:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

VStack {
  Picker("items", selection: $arg1) {
    ForEach($handler.items, id: \.self) {
      Text($0)
    }
  }
}

However, it fails to compile due to the following reason :

Initializer 'init(_:)' requires that 'Binding<String>' conform to 'StringProtocol'

Any Idea how to resolve this ?

>Solution :

You don’t need binding here to loop items, instead use handler directly (ie. without $), like

ForEach(handler.items, id: \.self) {    // << here !!
  Text($0)
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading