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