實例方法
zip(_:
zip(_:_:_:_:)
結合其他三個發布者的元素並提供轉換後的輸出。
聲明
func zip<P, Q, R, T>(
_ publisher1: P,
_ publisher2: Q,
_ publisher3: R,
_ transform: @escaping (Self.Output, P.Output, Q.Output, R.Output) -> T
) -> Publishers.Map<Publishers.Zip4<Self, P, Q, R>, T> where P : Publisher, Q : Publisher, R : Publisher, Self.Failure == P.Failure, P.Failure == Q.Failure, Q.Failure == R.Failure
返回值
使用 transform
閉包發出新元素的發布者,通過組合來自四個上遊發布者的最新值生成。
參數
publisher1
第二個發布者。
publisher2
第三個發布者。
publisher3
第四發布者。
transform
從每個發布者接收最新值並返回要發布的新值的閉包。
詳述
使用 Publisher/zip(_:_:_:_:)
返回一個新發布者,該發布者使用您指定的轉換組合來自其他三個發布者的元素,以將新值發布到下遊訂閱者。返回的發布者等到所有四個發布者都發出了一個事件,然後將來自每個發布者的最舊的未使用事件一起交付給操作符在轉換中使用的事件。
在此示例中,PassthroughSubject
發布者、numbersPub
、fractionsPub
、lettersPub
和 emojiPub
發出值。 Publisher/zip(_:_:_:_:)
運算符接收來自每個發布者的最舊值,並使用來自 numbersPub
的 Int
,並發布一個重複來自 lettersPub
和 emojiPub
的
多次的字符串,並打印出String
fractionsPub
中的值。
let numbersPub = PassthroughSubject<Int, Never>() // first publisher
let lettersPub = PassthroughSubject<String, Never>() // second
let emojiPub = PassthroughSubject<String, Never>() // third
let fractionsPub = PassthroughSubject<Double, Never>()// fourth
cancellable = numbersPub
.zip(lettersPub, emojiPub, fractionsPub) { anInt, aLetter, anEmoji, aFraction in
("\(String(repeating: anEmoji, count: anInt)) \(String(repeating: aLetter, count: anInt)) \(aFraction)")
}
.sink { print("\($0)") }
numbersPub.send(1) // numbersPub: 1 lettersPub: emojiPub: zip output: <none>
numbersPub.send(2) // numbersPub: 1,2 lettersPub: emojiPub: zip output: <none>
numbersPub.send(3) // numbersPub: 1,2,3 lettersPub: emojiPub: zip output: <none>
fractionsPub.send(0.1) // numbersPub: 1,2,3 lettersPub: "A" emojiPub: zip output: <none>
lettersPub.send("A") // numbersPub: 1,2,3 lettersPub: "A" emojiPub: zip output: <none>
emojiPub.send("😀") // numbersPub: 1,2,3 lettersPub: "A" emojiPub:"😀" zip output: "😀 A"
lettersPub.send("B") // numbersPub: 2,3 lettersPub: "B" emojiPub: zip output: <none>
fractionsPub.send(0.8) // numbersPub: 2,3 lettersPub: "A" emojiPub: zip output: <none>
emojiPub.send("🥰") // numbersPub: 3 lettersPub: "B" emojiPub: zip output: "🥰🥰 BB"
// Prints:
//1 😀 A 0.1
//2 🥰🥰 BB 0.8
如果任何上遊發布者成功完成或因錯誤而失敗,壓縮發布者也是如此。
可用版本
iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, watchOS 6.0+
相關用法
- Swift Optional.Publisher zip(_:_:_:)用法及代碼示例
- Swift Optional.Publisher zip(_:_:)用法及代碼示例
- Swift Optional.Publisher zip(_:)用法及代碼示例
- Swift Optional.Publisher reduce(_:_:)用法及代碼示例
- Swift Optional.Publisher tryDrop(while:)用法及代碼示例
- Swift Optional.Publisher debounce(for:scheduler:options:)用法及代碼示例
- Swift Optional.Publisher breakpoint(receiveSubscription:receiveOutput:receiveCompletion:)用法及代碼示例
- Swift Optional.Publisher mapError(_:)用法及代碼示例
- Swift Optional.Publisher catch(_:)用法及代碼示例
- Swift Optional.Publisher tryAllSatisfy(_:)用法及代碼示例
- Swift Optional.Publisher sink(receiveValue:)用法及代碼示例
- Swift Optional.Publisher scan(_:_:)用法及代碼示例
- Swift Optional.Publisher throttle(for:scheduler:latest:)用法及代碼示例
- Swift Optional.Publisher assertNoFailure(_:file:line:)用法及代碼示例
- Swift Optional.Publisher collect(_:)用法及代碼示例
- Swift Optional.Publisher combineLatest(_:_:_:_:)用法及代碼示例
- Swift Optional.Publisher share()用法及代碼示例
- Swift Optional.Publisher merge(with:_:_:)用法及代碼示例
- Swift Optional.Publisher map(_:_:)用法及代碼示例
- Swift Optional.Publisher assign(to:on:)用法及代碼示例
- Swift Optional.Publisher output(in:)用法及代碼示例
- Swift Optional.Publisher tryReduce(_:_:)用法及代碼示例
- Swift Optional.Publisher map(_:_:_:)用法及代碼示例
- Swift Optional.Publisher prepend(_:)用法及代碼示例
- Swift Optional.Publisher removeDuplicates()用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Optional.Publisher zip(_:_:_:_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。