SwiftUI Text.onReceive 抛出实例方法“onReceive(_:perform:)”要求“AnyCancellable”符合“Publisher”

问题描述

在 SwiftUI View ContentView 中,我有一个用于更新 Text 对象的通知发布:

struct ContentView: View {
...
let activePub = NotificationCenter.default.publisher(for: Notifications.ActiveEnergyBurnedAvailable).sink {_ in
}

var body: some View {
    Text("Hello,world!")
        .padding()
    vstack{
        Text("\(activeEnergyBurned)").onReceive(activePub) { _ in
            self.displayActiveEnergyBurned()
        }
        ...
    }
}

}

Text("(activeEnergyBurned)") 抛出 Instance method 'onReceive(_:perform:)' requires that 'AnyCancellable' conform to 'Publisher',因此当 Notifications.ActiveEnergyBurnedAvailable 通知被发布时,我似乎必须使用其他一些技术来更新 Text 对象。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)