用法一
zip(_:_:_:)
聲明
func zip<P, Q, T>(
_ publisher1: P,
_ publisher2: Q,
_ transform: @escaping (Self.Output, P.Output, Q.Output) -> T
) -> Publishers.Map<Publishers.Zip3<Self, P, Q>, T> where P : Publisher, Q : Publisher, Self.Failure == P.Failure, P.Failure == Q.Failure
返回值
使用 transform
閉包發出新元素的發布者,通過組合來自三個上遊發布者的最新值生成。
參數
publisher1
第二個發布者。
publisher2
第三個發布者。
transform
從每個發布者接收最新值並返回要發布的新值的閉包。
詳述
使用 Publisher/zip(_:_:_:)-9yqi1
返回一個新發布者,該發布者使用您指定的轉換組合來自其他兩個發布者的元素,以將新值發布到下遊訂閱者。返回的發布者等到所有三個發布者都發出了一個事件,然後將來自每個發布者的最舊的未使用事件一起傳遞給操作符在轉換中使用的事件。
在此示例中,numbersPub
、lettersPub
和 emojiPub
每個都是發出值的 PassthroughSubject
; Publisher/zip(_:_:_:)-9yqi1
接收來自每個發布者的最舊值,並使用來自 numbersPub
的 Int
並發布一個重複來自 lettersPub
和 emojiPub
的
多次的字符串。String
let numbersPub = PassthroughSubject<Int, Never>()
let lettersPub = PassthroughSubject<String, Never>()
let emojiPub = PassthroughSubject<String, Never>()
cancellable = numbersPub
.zip(letters, emoji) { anInt, aLetter, anEmoji in
("\(String(repeating: anEmoji, count: anInt)) \(String(repeating: aLetter, count: anInt))")
}
.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>
lettersPub.send("A") // numbersPub: 1,2,3 lettersPub: "A" emojiPub: zip output: <none>
emojiPub.send("😀") // numbersPub: 2,3 lettersPub: "A" emojiPub:"😀" zip output: "😀 A"
lettersPub.send("B") // numbersPub: 2,3 lettersPub: "B" emojiPub: zip output: <none>
emojiPub.send("🥰") // numbersPub: 3 lettersPub: emojiPub:"😀", "🥰" zip output: "🥰🥰 BB"
// Prints:
// 😀 A
// 🥰🥰 BB
如果任何上遊發布者成功完成或因錯誤而失敗,壓縮發布者也是如此。
可用版本
用法二
zip(_:_:_:)
聲明
func zip<P, Q, R>(
_ publisher1: P,
_ publisher2: Q,
_ publisher3: R
) -> Publishers.Zip4<Self, P, Q, R> where P : Publisher, Q : Publisher, R : Publisher, Self.Failure == P.Failure, P.Failure == Q.Failure, Q.Failure == R.Failure
返回值
從上遊發布者發出元素組作為元組的發布者。
參數
publisher1
第二個發布者。
publisher2
第三個發布者。
publisher3
第四發布者。
詳述
使用 Publisher/zip(_:_:_:)-16rcy
返回一個新的發布者,該發布者將來自其他三個發布者的元素組合起來,以將一個元組發布到下遊訂閱者。返回的發布者等到所有四個發布者都發出了一個事件,然後將每個發布者中最舊的未使用事件作為一個元組傳遞給訂閱者。
在此示例中,幾個 PassthroughSubject
實例發出值; Publisher/zip(_:_:_:)-16rcy
從每個發布者接收最舊的未使用值,並將它們組合成一個元組,然後重新發布到下遊:
let numbersPub = PassthroughSubject<Int, Never>()
let lettersPub = PassthroughSubject<String, Never>()
let emojiPub = PassthroughSubject<String, Never>()
let fractionsPub = PassthroughSubject<Double, Never>()
cancellable = numbersPub
.zip(lettersPub, emojiPub, fractionsPub)
.sink { print("\($0)") }
numbersPub.send(1) // numbersPub: 1 lettersPub: emojiPub: fractionsPub: zip output: <none>
numbersPub.send(2) // numbersPub: 1,2 lettersPub: emojiPub: fractionsPub: zip output: <none>
numbersPub.send(3) // numbersPub: 1,2,3 lettersPub: emojiPub: fractionsPub: zip output: <none>
fractionsPub.send(0.1) // numbersPub: 1,2,3 lettersPub: "A" emojiPub: fractionsPub: 0.1 zip output: <none>
lettersPub.send("A") // numbersPub: 1,2,3 lettersPub: "A" emojiPub: fractionsPub: 0.1 zip output: <none>
emojiPub.send("😀") // numbersPub: 2,3 lettersPub: "A" emojiPub: "😀" fractionsPub: 0.1 zip output: (1, "A", "😀", 0.1)
lettersPub.send("B") // numbersPub: 2,3 lettersPub: "B" emojiPub: fractionsPub: zip output: <none>
fractionsPub.send(0.8) // numbersPub: 2,3 lettersPub: "B" emojiPub: fractionsPub: 0.8 zip output: <none>
emojiPub.send("🥰") // numbersPub: 3 lettersPub: "B" emojiPub: fractionsPub: 0.8 zip output: (2, "B", "🥰", 0.8)
// Prints:
// (1, "A", "😀", 0.1)
// (2, "B", "🥰", 0.8)
如果任何上遊發布者成功完成或因錯誤而失敗,壓縮發布者也是如此。
可用版本
相關用法
- 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(_:_:_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。