实例方法
merge(with:
merge(with:_:)
将来自该发布者的元素与来自其他两个发布者的元素组合在一起,提供交错的元素序列。
声明
func merge<B, C>(
with b: B,
_ c: C
) -> Publishers.Merge3<Self, B, C> where B : Publisher, C : Publisher, Self.Failure == B.Failure, Self.Output == B.Output, B.Failure == C.Failure, B.Output == C.Output
返回值
当任何上游发布者发出事件时发出事件的发布者。
参数
b
第二个发布者。
c
第三个发布者。
详述
当您想要在任何上游发布者发出元素时接收新元素时,请使用 Publisher/merge(with:_:)
。要在所有上游发布者发出值时从所有上游发布者那里接收最新值的元组,请使用 Publisher/combineLatest(_:_:)-5crqg
。要组合来自多个上游发布者的元素,请使用 Publisher/zip(_:_:)-8d7k7
。
在此示例中,当 Publisher/merge(with:_:)
从上游发布者接收输入时,它会将交错的元素重新发布到下游:
let pubA = PassthroughSubject<Int, Never>()
let pubB = PassthroughSubject<Int, Never>()
let pubC = PassthroughSubject<Int, Never>()
cancellable = pubA
.merge(with: pubB, pubC)
.sink { print("\($0)", terminator: " " )}
pubA.send(1)
pubB.send(40)
pubC.send(90)
pubA.send(2)
pubB.send(50)
pubC.send(100)
// Prints: "1 40 90 2 50 100"
合并的发布者继续发出元素,直到所有上游发布者完成。如果上游发布者产生错误,则合并的发布者会因该错误而失败。
可用版本
iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, watchOS 6.0+
相关用法
- Swift Result.Publisher merge(with:_:_:)用法及代码示例
- Swift Result.Publisher merge(with:_:_:_:_:_:_:)用法及代码示例
- Swift Result.Publisher merge(with:_:_:_:_:_:)用法及代码示例
- Swift Result.Publisher merge(with:_:_:_:_:)用法及代码示例
- Swift Result.Publisher merge(with:_:_:_:)用法及代码示例
- Swift Result.Publisher measureInterval(using:options:)用法及代码示例
- Swift Result.Publisher map(_:_:_:)用法及代码示例
- Swift Result.Publisher map(_:)用法及代码示例
- Swift Result.Publisher min()用法及代码示例
- Swift Result.Publisher multicast(_:)用法及代码示例
- Swift Result.Publisher multicast(subject:)用法及代码示例
- Swift Result.Publisher max()用法及代码示例
- Swift Result.Publisher map(_:_:)用法及代码示例
- Swift Result.Publisher mapError(_:)用法及代码示例
- Swift Result.Publisher makeConnectable()用法及代码示例
- Swift Result.Publisher zip(_:_:_:)用法及代码示例
- Swift Result.Publisher tryLast(where:)用法及代码示例
- Swift Result.Publisher sink(receiveCompletion:receiveValue:)用法及代码示例
- Swift Result.Publisher tryCompactMap(_:)用法及代码示例
- Swift Result.Publisher print(_:to:)用法及代码示例
- Swift Result.Publisher sink(receiveValue:)用法及代码示例
- Swift Result.Publisher eraseToAnyPublisher()用法及代码示例
- Swift Result.Publisher setFailureType(to:)用法及代码示例
- Swift Result.Publisher first(where:)用法及代码示例
- Swift Result.Publisher output(at:)用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 Result.Publisher merge(with:_:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。