实例方法
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 AsyncThrowingFlatMapSequence first(where:)用法及代码示例
- Swift AsyncThrowingFlatMapSequence flatMap(_:)用法及代码示例
- Swift AsyncThrowingFlatMapSequence min()用法及代码示例
- Swift AsyncThrowingFlatMapSequence allSatisfy(_:)用法及代码示例
- Swift AsyncThrowingFlatMapSequence reduce(_:_:)用法及代码示例
- Swift AsyncThrowingFlatMapSequence prefix(while:)用法及代码示例
- Swift AsyncThrowingFlatMapSequence compactMap(_:)用法及代码示例
- Swift AsyncThrowingFlatMapSequence dropFirst(_:)用法及代码示例
- Swift AsyncThrowingFlatMapSequence prefix(_:)用法及代码示例
- Swift AsyncThrowingFlatMapSequence map(_:)用法及代码示例
- Swift AsyncThrowingFlatMapSequence max(by:)用法及代码示例
- Swift AsyncThrowingFlatMapSequence drop(while:)用法及代码示例
- Swift AsyncThrowingFlatMapSequence contains(_:)用法及代码示例
- Swift AsyncThrowingFlatMapSequence min(by:)用法及代码示例
- Swift AsyncThrowingFlatMapSequence max()用法及代码示例
- Swift AsyncThrowingFlatMapSequence contains(where:)用法及代码示例
- 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 prefix(_:)用法及代码示例
- Swift AsyncThrowingFilterSequence prefix(while:)用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 AsyncThrowingFlatMapSequence filter(_:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。