实例方法
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 Optional.Publisher sink(receiveValue:)用法及代码示例
- Swift Optional.Publisher scan(_:_:)用法及代码示例
- Swift Optional.Publisher share()用法及代码示例
- Swift Optional.Publisher setFailureType(to:)用法及代码示例
- Swift Optional.Publisher subscribe(on:options:)用法及代码示例
- 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 zip(_:_:_:)用法及代码示例
- Swift Optional.Publisher throttle(for:scheduler:latest:)用法及代码示例
- Swift Optional.Publisher assertNoFailure(_:file:line:)用法及代码示例
- Swift Optional.Publisher collect(_:)用法及代码示例
- Swift Optional.Publisher combineLatest(_:_:_:_:)用法及代码示例
- Swift Optional.Publisher merge(with:_:_:)用法及代码示例
- Swift Optional.Publisher map(_:_:)用法及代码示例
- Swift Optional.Publisher assign(to:on:)用法及代码示例
- Swift Optional.Publisher zip(_:_:_:_:)用法及代码示例
- Swift Optional.Publisher output(in:)用法及代码示例
- Swift Optional.Publisher tryReduce(_:_:)用法及代码示例
- Swift Optional.Publisher map(_:_:_:)用法及代码示例
- Swift Optional.Publisher prepend(_:)用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 Optional.Publisher sink(receiveCompletion:receiveValue:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。