實例方法
sink(receive
sink(receiveCompletion:receiveValue:)
使用基於閉包的行為附加訂閱者。
聲明
func sink(
receiveCompletion: @escaping ((Subscribers.Completion<Self.Failure>) -> Void),
receiveValue: @escaping ((Self.Output) -> Void)
) -> AnyCancellable
返回值
一個可取消的實例,當您結束接收值的分配時使用它。結果的重新分配將關閉訂閱流。
參數
receiveComplete
完成時執行的閉包。
receiveValue
在收到值時執行的關閉。
詳述
使用 Publisher/sink(receiveCompletion:receiveValue:)
觀察發布者收到的值並使用您指定的閉包處理它們。
在此示例中,
發布者將整數發布到 Range
Publisher/sink(receiveCompletion:receiveValue:)
運算符的 receiveValue
閉包,該閉包將它們打印到控製台。完成後,Publisher/sink(receiveCompletion:receiveValue:)
運算符的 receiveCompletion
閉包表示流的成功終止。
let myRange = (0...3)
cancellable = myRange.publisher
.sink(receiveCompletion: { print ("completion: \($0)") },
receiveValue: { print ("value: \($0)") })
// Prints:
// value: 0
// value: 1
// value: 2
// value: 3
// completion: finished
此方法創建訂閱者並在返回訂閱者之前立即請求無限數量的值。應保留返回值,否則將取消流。
可用版本
iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, watchOS 6.0+
相關用法
- Swift Result.Publisher sink(receiveValue:)用法及代碼示例
- Swift Result.Publisher setFailureType(to:)用法及代碼示例
- Swift Result.Publisher subscribe(on:options:)用法及代碼示例
- Swift Result.Publisher share()用法及代碼示例
- Swift Result.Publisher scan(_:_:)用法及代碼示例
- Swift Result.Publisher zip(_:_:_:)用法及代碼示例
- Swift Result.Publisher tryLast(where:)用法及代碼示例
- Swift Result.Publisher merge(with:_:_:)用法及代碼示例
- Swift Result.Publisher tryCompactMap(_:)用法及代碼示例
- Swift Result.Publisher print(_:to:)用法及代碼示例
- Swift Result.Publisher eraseToAnyPublisher()用法及代碼示例
- 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 catch(_:)用法及代碼示例
- Swift Result.Publisher drop(while:)用法及代碼示例
- Swift Result.Publisher prefix(_:)用法及代碼示例
- Swift Result.Publisher timeout(_:scheduler:options:customError:)用法及代碼示例
- Swift Result.Publisher merge(with:_:)用法及代碼示例
- Swift Result.Publisher map(_:)用法及代碼示例
- Swift Result.Publisher append(_:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Result.Publisher sink(receiveCompletion:receiveValue:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。