Reputation: 36058
I'm trying to bind properties together in the view and couldn't find anything better specifically for it. This is what I'm doing:
struct ContentView: View {
@ObservedObject var model: MyModel
@State var selectedID: Int
var body: some View {
Picker("Choose", selection: $selectedID) {
Text("Abc").tag(0)
Text("Def").tag(1)
Text("Ghi").tag(2)
}
.onChange(of: model.item?.selectedID) {
selectedID = $0
}
}
}
Is there a better way to bind properties together?
Upvotes: 1
Views: 682
Reputation: 257493
If it is about unidirectional flow then the only change I see needed in provided snapshot is make similar types. Everything else is ok.
struct ContentView: View {
@ObservedObject var model: MyModel
@State var selectedID: Int? // << make optional as well !!
...
Upvotes: 1