combineLatest
combineLatest 操作符的基本用法
combineLatest 操作符的基本用法func combineLatest<P: Publisher>(_ other: P) -> Publishers.CombineLatest<Self, P>示例:两个 Publisher 的 combineLatest
Publisher 的 combineLatestimport Combine
let publisher1 = PassthroughSubject<Int, Never>()
let publisher2 = PassthroughSubject<String, Never>()
let combinedSubscription = publisher1
.combineLatest(publisher2) // 合并两个 Publisher 的最新值
.sink { (intValue, stringValue) in
print("Int: \(intValue), String: \(stringValue)")
}
publisher1.send(1)
publisher2.send("A")
publisher1.send(2)
publisher2.send("B")输出结果:
示例:多个 Publisher 使用 combineLatest
Publisher 使用 combineLatest输出结果:
工作原理
示例:完成和错误处理
输出结果:
与 zip 的区别
zip 的区别总结
Last updated