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