實例方法
collect(_:)
收集最多指定數量的元素,然後發出集合的單個數組。
聲明
func collect(_ count: Int) -> Publishers.CollectByCount<Self>
返回值
一個發布者,最多收集指定數量的元素,然後將它們作為數組發布。
參數
count
發布前要緩衝的最大接收元素數。
詳述
使用 Publisher/collect(_:)
從上遊發布者發出最多包含 count
元素的數組。如果上遊發布者在收集指定數量的元素之前完成,則發布者發送一個僅包含它接收到的項目的數組。這可能少於count
元素。
如果上遊發布者因錯誤而失敗,則此發布者將錯誤轉發給下遊接收者,而不是發送其輸出。
在下麵的示例中,Publisher/collect(_:)
運算符根據請求的集合大小 5
發出一個部分數組和兩個完整數組:
let numbers = (0...10)
cancellable = numbers.publisher
.collect(5)
.sink { print("\($0), terminator: " "") }
// Prints "[0, 1, 2, 3, 4] [5, 6, 7, 8, 9] [10] "
可用版本
iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, watchOS 6.0+
相關用法
- Swift Result.Publisher collect(_:options:)用法及代碼示例
- Swift Result.Publisher combineLatest(_:_:)用法及代碼示例
- Swift Result.Publisher combineLatest(_:_:_:_:)用法及代碼示例
- Swift Result.Publisher contains(_:)用法及代碼示例
- Swift Result.Publisher combineLatest(_:_:_:)用法及代碼示例
- Swift Result.Publisher compactMap(_:)用法及代碼示例
- Swift Result.Publisher combineLatest(_:)用法及代碼示例
- Swift Result.Publisher catch(_:)用法及代碼示例
- Swift Result.Publisher zip(_:_:_:)用法及代碼示例
- Swift Result.Publisher tryLast(where:)用法及代碼示例
- Swift Result.Publisher sink(receiveCompletion:receiveValue:)用法及代碼示例
- Swift Result.Publisher merge(with:_:_:)用法及代碼示例
- 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:)用法及代碼示例
- Swift Result.Publisher tryCatch(_:)用法及代碼示例
- Swift Result.Publisher dropFirst(_:)用法及代碼示例
- Swift Result.Publisher merge(with:_:_:_:_:_:_:)用法及代碼示例
- Swift Result.Publisher flatMap(maxPublishers:_:)用法及代碼示例
- Swift Result.Publisher map(_:_:_:)用法及代碼示例
- Swift Result.Publisher drop(while:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Result.Publisher collect(_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。