实例方法
sink(receive
sink(receiveValue:)
将具有基于闭包行为的订阅者附加到永不失败的发布者。
声明
func sink(receiveValue: @escaping ((Self.Output) -> Void)) -> AnyCancellable
当
Failure
为 Never
时可用。返回值
一个可取消的实例,当您结束接收值的分配时使用它。结果的重新分配将关闭订阅流。
参数
receiveValue
在收到值时执行的关闭。
详述
使用Publisher/sink(receiveValue:)
观察发布者收到的值并将它们打印到控制台。该操作符只能在流不失败的情况下使用,即发布者的 Publisher/Failure
类型为
时。Never
在此示例中,
发布者将整数发布到 Range
Publisher/sink(receiveValue:)
运算符的 receiveValue
闭包,该闭包将它们打印到控制台:
let integers = (0...3)
integers.publisher
.sink { print("Received \($0)") }
// Prints:
// Received 0
// Received 1
// Received 2
// Received 3
此方法创建订阅者并在返回订阅者之前立即请求无限数量的值。应保留返回值,否则将取消流。
可用版本
iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, watchOS 6.0+
相关用法
- Swift Result.Publisher sink(receiveCompletion: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(receiveValue:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。