实例方法
prefix(while:)
重新发布元素,而谓词闭包表示应该继续发布。
声明
func prefix(while predicate: @escaping (Self.Output) -> Bool) -> Publishers.PrefixWhile<Self>
返回值
传递元素直到谓词指示发布应该完成的发布者。
参数
predicate
一个闭包,它以一个元素作为其参数并返回一个布尔值,指示是否应该继续发布。
详述
当上游发布者的元素满足您指定的条件时,使用 Publisher/prefix(while:)
发出值。当闭包返回 false
时,发布者完成。
在下面的示例中,Publisher/prefix(while:)
运算符在其接收的元素小于 5 时发出值:
let numbers = (0...10)
numbers.publisher
.prefix { $0 < 5 }
.sink { print("\($0)", terminator: " ") }
// Prints: "0 1 2 3 4"
可用版本
iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, watchOS 6.0+
相关用法
- Swift Result.Publisher prefix(_:)用法及代码示例
- Swift Result.Publisher prepend(_:)用法及代码示例
- Swift Result.Publisher print(_:to:)用法及代码示例
- 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 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 catch(_:)用法及代码示例
- Swift Result.Publisher drop(while:)用法及代码示例
- Swift Result.Publisher timeout(_:scheduler:options:customError:)用法及代码示例
- Swift Result.Publisher merge(with:_:)用法及代码示例
- Swift Result.Publisher map(_:)用法及代码示例
- Swift Result.Publisher append(_:)用法及代码示例
- Swift Result.Publisher breakpointOnError()用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 Result.Publisher prefix(while:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。