有没有办法在 Swift Combine 中多次调用 assign()?

Is there a way to call assign() more than once in Swift Combine?

我想用处理新发布的值时收到的值更新一些变量。例如,给定:

class ProductViewModel: ObservableObject {
    @Published var PublishedX: Int = 0
    @Published var PublishedY: Int = 0
    @Published var PublishedProduct: Int = 0
    // ...
    init() {
        productPublisher = Publishers.CombineLatest(external.XPublisher, internal.YPublisher)
            // .assignAndContinue(\.PublishedX, \.PublishedY) // something like this
            .flatMap(MyPublishers.secretMultiplication)
            .assign(to: \.PublishedProduct, on: self)
     }
 }

我还想将 XPublisher 和 YPublisher 的新值分配给变量(分别为 PublishedX 和 PublishedY)。

有没有办法设置这两个变量,然后继续处理事件?

更新

如果您的部署目标是 2020 系统(如 iOS 14+ 或 macOS 11+),您可以使用 a different version of the assign operator 来避免保留循环并避免存储可取消项:

init() {
    external.XPublisher.assign(to: $PublishedX)
    external.YPublisher.assign(to: $PublishedY)
    external.XPublisher
        .combineLatest(external.YPublisher) { [=10=] *  }
        .assign(to: $PublishedProduct)
}

原创

没有 assign(to:on:) 的内置变体 returns 另一个 Publisher 而不是 Cancellable

只需使用多个 assigns:

class ProductViewModel: ObservableObject {
    @Published var PublishedX: Int = 0
    @Published var PublishedY: Int = 0
    @Published var PublishedProduct: Int = 0

    init() {
        external.XPublisher
            .assign(to: \.PublishedX, on: self)
            .store(in: &tickets)
        internal.YPublisher
            .assign(to: \.PublishedY, on: self)
            .store(in: &tickets)
        external.XPublisher
            .combineLatest(internal.YPublisher) { [=11=] *  }
            .assign(to: \.PublishedProduct, on: self)
            .store(in: &tickets)
    }

    private var tickets: [AnyCancellable] = []
}

请注意,这些订阅会创建保留周期。 Swift 将无法销毁 ProductViewModel 的实例,直到 tickets 数组被清除。 (这不是我的建议属性。你的原始代码也需要在某个地方存储它的订阅,否则它会立即被取消。)

此外,PublishedProduct 的存在值得怀疑。为什么不只是计算 属性?

var product: Int { PublishedX * PublishedY }

您可能想要check out this library

来自他们的ReadMe

var label1: UILabel
var label2: UILabel
var text: UITextField

["hey", "there", "friend"]
    .publisher
    .assign(to: \.text, on: label1,
            and: \.text, on: label2,
            and: \.text, on: text)