从 ObservableObject 发布者更新状态

Updating State from ObservableObject publisher

我正在尝试使用 Combine 和 SwiftUI 但基本上停留在更新状态我想在每次 ObservableObject 更改时更新视图中的状态, 这是例子。

class XViewModel: ObservableObject {
    @Published var tVal: Bool = false
    private var cancellables = Set<AnyCancellable>()

    func change() {
        Just(true)
            .delay(for: 3.0, scheduler: RunLoop.main)
            .receive(on: RunLoop.main)
            .assign(to: \.tVal, on: self)
            .store(in: &self.cancellables)
    }
}

我有 viewModel 和一个发布者以及延迟发布者,它在 3 秒后触发。

struct ContentView: View {
    @ObservedObject var viewModel = XViewModel()
    @State var toggleVal: Bool = false
    private var cancellables = Set<AnyCancellable>()

    init() {
        self.viewModel.$tVal
            .sink { newVal in
            print(newVal)
        }
        .store(in: &cancellables)        

        self.viewModel
            .$tVal.assign(to: \.toggleVal, on: self)
            .store(in: &cancellables)

        viewModel.change()
    }

    var body: some View {
        VStack {
            Toggle(isOn: self.$viewModel.tVal) {
                Text("Toggle")

            Toggle(isOn: self.$toggleVal) {
                Text("Toggle from View")
            }
    }

}

我期望的是

viewModel.Just triggers
viewModel.tVal publisher triggers
view.toggleVal state triggers 
updates UI 

但似乎虽然所有内容都已更新,但似乎并没有更新状态。有什么方法可以更新 State 或者它根本不打算更新,我需要将我的视图直接绑定到 viewModel 的 tVal 值,它是发布者。

谢谢。

@Stateinit 中还没有准备好运行,请改用 .onReceive,如下所示。

struct ContentView: View {
    @ObservedObject var viewModel = XViewModel()
    @State var toggleVal: Bool = false
    private var cancellables = Set<AnyCancellable>()

    init() {
        self.viewModel.$tVal
            .sink { newVal in
            print(newVal)
        }
        .store(in: &cancellables)        

        viewModel.change()
    }

    var body: some View {
        VStack {
            Toggle(isOn: self.$viewModel.tVal) {
                Text("Toggle")

            Toggle(isOn: self.$toggleVal) {
                Text("Toggle from View")
            }
            .onReceive(self.viewModel.$tVal) { newVal in // << here !!
                self.toggleVal = newVal
            }
    }

}