在我的viewModel中,我有的是CurrentValueSubject,有的是PassthroughSubject。
如何将它们绑定到SwiftU中,其中视图期望输入@Binding<Type>类型。
我试过了,但效果不太好:
extension CurrentValueSubject {
var binding: Binding<Output> {
Binding(get: {
self.value
}, set: {
self.send($0)
})
}
}发布于 2022-09-05 15:44:24
可以使用.onReceive修饰符,如下所示:
// View Model
class MyVM {
var sub: CurrentValueSubject<String, Never>
}
// View
struct MyView: View {
var body: some View {
Text("Hi..")
.onReceive(viewModel.sub) { newValue in
// Here create binding or assign it to @State variable
}
}
}https://stackoverflow.com/questions/73610738
复制相似问题