You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SwiftUI报错:Generic struct 'Merge3'要求'Binding<Double>'遵循'Publisher'

问题原因与解决方案

报错原因

Publishers.Merge3要求三个输入参数必须是符合Publisher协议的类型,但你直接传入的Binding<Double>(即$value1这类对象)本身并不属于Publisher,所以触发了类型不兼容的错误。

解决方法

SwiftUI的Binding自带了.publisher属性,这个属性会返回一个Publisher实例——当Binding对应的值发生变化时,该Publisher会自动发送最新的值。你只需要把每个Binding转换为对应的Publisher,就能正常使用Merge3合并流了。

修改后的calculateOnChange方法代码如下:

private func calculateOnChange() -> AnyPublisher<Void, Never> {
    Publishers.Merge3($value1.publisher, $value2.publisher, $value3.publisher)
       .map { _ in () }
       .eraseToAnyPublisher()
}

补充说明

如果需要在值变化时获取具体的新值,也可以调整map的逻辑,比如:

.map { value in
    print("当前变化的值:\(value)")
    return ()
}

内容的提问来源于stack exchange,提问作者GettingStarted

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 12:00:59