我正在尝试在视图中将属性绑定在一起,但找不到更好的方法。这就是我要做的:
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 } } }
有没有更好的方法将属性绑定在一起?
如果是关于 单向流 那么,我在提供的快照中看到的唯一需要的更改是使类型相似。其他都没问题。
struct ContentView: View { @ObservedObject var model: MyModel @State var selectedID: Int? // << make optional as well !! ...