实例方法
prefix(while:)
返回一个异步序列,包含满足给定谓词的基本序列的初始连续元素。
声明
@preconcurrency func prefix(while predicate: @escaping (Self.Element) async -> Bool) rethrows -> AsyncPrefixWhileSequence<Self>
返回值
满足 predicate
的初始连续元素的异步序列。
参数
predicate
一个闭包,它将元素作为参数并返回一个布尔值,指示该元素是否应包含在修改后的序列中。
详述
当基本序列中的元素满足您指定的条件时,使用prefix(while:)
生成值。当谓词闭包返回 false
时,修改后的序列结束。
在此示例中,名为 Counter
的异步序列生成从 1
到 10
的 Int
值。 prefix(while:)
方法会导致修改后的序列传递值,只要它们不能被 2
和 3
整除。到达 6
后,序列结束:
let stream = Counter(howHigh: 10)
.prefix { $0 % 2 != 0 || $0 % 3 != 0 }
for try await number in stream {
print("\(number) ", terminator: " ")
}
// prints "1 2 3 4 5"
可用版本
iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, watchOS 6.0+
相关用法
- Swift AsyncThrowingFilterSequence prefix(_:)用法及代码示例
- Swift AsyncThrowingFilterSequence drop(while:)用法及代码示例
- Swift AsyncThrowingFilterSequence max(by:)用法及代码示例
- Swift AsyncThrowingFilterSequence reduce(_:_:)用法及代码示例
- Swift AsyncThrowingFilterSequence min(by:)用法及代码示例
- Swift AsyncThrowingFilterSequence map(_:)用法及代码示例
- Swift AsyncThrowingFilterSequence max()用法及代码示例
- Swift AsyncThrowingFilterSequence contains(_:)用法及代码示例
- Swift AsyncThrowingFilterSequence min()用法及代码示例
- Swift AsyncThrowingFilterSequence filter(_:)用法及代码示例
- Swift AsyncThrowingFilterSequence compactMap(_:)用法及代码示例
- Swift AsyncThrowingFilterSequence flatMap(_:)用法及代码示例
- Swift AsyncThrowingFilterSequence contains(where:)用法及代码示例
- Swift AsyncThrowingFilterSequence allSatisfy(_:)用法及代码示例
- Swift AsyncThrowingFilterSequence dropFirst(_:)用法及代码示例
- Swift AsyncThrowingFilterSequence first(where:)用法及代码示例
- Swift AsyncThrowingFlatMapSequence min()用法及代码示例
- Swift AsyncThrowingFlatMapSequence allSatisfy(_:)用法及代码示例
- Swift AsyncThrowingFlatMapSequence first(where:)用法及代码示例
- Swift AsyncThrowingFlatMapSequence reduce(_:_:)用法及代码示例
- Swift AsyncThrowingFlatMapSequence prefix(while:)用法及代码示例
- Swift AsyncThrowingFlatMapSequence compactMap(_:)用法及代码示例
- Swift AsyncThrowingFlatMapSequence dropFirst(_:)用法及代码示例
- Swift AsyncThrowingFlatMapSequence filter(_:)用法及代码示例
- Swift AsyncThrowingFlatMapSequence prefix(_:)用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 AsyncThrowingFilterSequence prefix(while:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。