1

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:

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 ?

1 Answer 1

1

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

ForEach(handler.items, id: \.self) {    // << here !!
  Text($0)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.