实例方法
filter(_:)
创建一个异步序列,该序列按顺序包含满足给定谓词的基本序列元素。
声明
@preconcurrency func filter(_ isIncluded: @escaping (Self.Element) async -> Bool) -> AsyncFilterSequence<Self>
返回值
按顺序包含满足给定谓词的基本序列元素的异步序列。
参数
isIncluded
一个闭包,它将异步序列的元素作为其参数并返回一个布尔值,该值指示是否在过滤后的序列中包含该元素。
详述
在此示例中,名为 Counter
的异步序列生成从 1
到 10
的 Int
值。 filter(_:)
方法为偶数返回true
,为奇数返回false
,从而过滤掉奇数:
let stream = Counter(howHigh: 10)
.filter { $0 % 2 == 0 }
for await number in stream {
print("\(number) ", terminator: " ")
}
// Prints: 2 4 6 8 10
可用版本
iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, watchOS 6.0+
相关用法
- Swift AsyncThrowingDropWhileSequence first(where:)用法及代码示例
- Swift AsyncThrowingDropWhileSequence flatMap(_:)用法及代码示例
- Swift AsyncThrowingDropWhileSequence allSatisfy(_:)用法及代码示例
- Swift AsyncThrowingDropWhileSequence max()用法及代码示例
- Swift AsyncThrowingDropWhileSequence contains(_:)用法及代码示例
- Swift AsyncThrowingDropWhileSequence min()用法及代码示例
- Swift AsyncThrowingDropWhileSequence prefix(_:)用法及代码示例
- Swift AsyncThrowingDropWhileSequence max(by:)用法及代码示例
- Swift AsyncThrowingDropWhileSequence dropFirst(_:)用法及代码示例
- Swift AsyncThrowingDropWhileSequence compactMap(_:)用法及代码示例
- Swift AsyncThrowingDropWhileSequence reduce(_:_:)用法及代码示例
- Swift AsyncThrowingDropWhileSequence prefix(while:)用法及代码示例
- Swift AsyncThrowingDropWhileSequence drop(while:)用法及代码示例
- Swift AsyncThrowingDropWhileSequence map(_:)用法及代码示例
- Swift AsyncThrowingDropWhileSequence contains(where:)用法及代码示例
- Swift AsyncThrowingDropWhileSequence min(by:)用法及代码示例
- Swift AsyncThrowingFlatMapSequence min()用法及代码示例
- Swift AsyncThrowingMapSequence reduce(_:_:)用法及代码示例
- Swift AsyncThrowingPrefixWhileSequence drop(while:)用法及代码示例
- Swift AsyncThrowingMapSequence contains(_:)用法及代码示例
- Swift AsyncThrowingPrefixWhileSequence filter(_:)用法及代码示例
- Swift AsyncThrowingCompactMapSequence drop(while:)用法及代码示例
- Swift AsyncThrowingFilterSequence drop(while:)用法及代码示例
- Swift AsyncThrowingCompactMapSequence prefix(while:)用法及代码示例
- Swift AsyncThrowingStream filter(_:)用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 AsyncThrowingDropWhileSequence filter(_:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。