實例方法
merge(with:
merge(with:_:_:_:_:)
將來自該發布者的元素與來自其他五個發布者的元素組合在一起,提供交錯的元素序列。
聲明
func merge<B, C, D, E, F>(
with b: B,
_ c: C,
_ d: D,
_ e: E,
_ f: F
) -> Publishers.Merge6<Self, B, C, D, E, F> where B : Publisher, C : Publisher, D : Publisher, E : Publisher, F : Publisher, Self.Failure == B.Failure, Self.Output == B.Output, B.Failure == C.Failure, B.Output == C.Output, C.Failure == D.Failure, C.Output == D.Output, D.Failure == E.Failure, D.Output == E.Output, E.Failure == F.Failure, E.Output == F.Output
返回值
當任何上遊發布者發出事件時發出事件的發布者。
參數
b
第二個發布者。
c
第三個發布者。
d
第四發布者。
e
第五發布者。
f
第六位發布者。
詳述
當您想要在任何上遊發布者發出元素時接收新元素時,請使用 Publisher/merge(with:_:_:_:_:_:)
。要在所有上遊發布者發出值時從所有上遊發布者那裏接收最新值的元組,請使用 Publisher/combineLatest(_:_:_:)-48buc
。要組合來自多個上遊發布者的元素,請使用 Publisher/zip(_:_:_:)-16rcy
。
在此示例中,當 Publisher/merge(with:_:_:_:_:_:)
從上遊發布者接收輸入時,它會將交錯的元素重新發布到下遊:
let pubA = PassthroughSubject<Int, Never>()
let pubB = PassthroughSubject<Int, Never>()
let pubC = PassthroughSubject<Int, Never>()
let pubD = PassthroughSubject<Int, Never>()
let pubE = PassthroughSubject<Int, Never>()
let pubF = PassthroughSubject<Int, Never>()
cancellable = pubA
.merge(with: pubB, pubC, pubD, pubE, pubF)
.sink { print("\($0)", terminator: " " ) }
pubA.send(1)
pubB.send(40)
pubC.send(90)
pubD.send(-1)
pubE.send(33)
pubF.send(44)
pubA.send(2)
pubB.send(50)
pubC.send(100)
pubD.send(-2)
pubE.send(33)
pubF.send(33)
//Prints: "1 40 90 -1 33 44 2 50 100 -2 33 33"
合並的發布者繼續發出元素,直到所有上遊發布者完成。如果上遊發布者產生錯誤,則合並的發布者會因該錯誤而失敗。
可用版本
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:_:_:_:_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。