實例方法
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 AsyncThrowingStream first(where:)用法及代碼示例
- Swift AsyncThrowingStream flatMap(_:)用法及代碼示例
- Swift AsyncThrowingStream init(unfolding:)用法及代碼示例
- Swift AsyncThrowingStream max(by:)用法及代碼示例
- Swift AsyncThrowingStream contains(where:)用法及代碼示例
- Swift AsyncThrowingStream map(_:)用法及代碼示例
- Swift AsyncThrowingStream prefix(_:)用法及代碼示例
- Swift AsyncThrowingStream min(by:)用法及代碼示例
- Swift AsyncThrowingStream allSatisfy(_:)用法及代碼示例
- Swift AsyncThrowingStream contains(_:)用法及代碼示例
- Swift AsyncThrowingStream compactMap(_:)用法及代碼示例
- Swift AsyncThrowingStream reduce(_:_:)用法及代碼示例
- Swift AsyncThrowingStream init(_:bufferingPolicy:_:)用法及代碼示例
- Swift AsyncThrowingStream drop(while:)用法及代碼示例
- Swift AsyncThrowingStream min()用法及代碼示例
- Swift AsyncThrowingStream prefix(while:)用法及代碼示例
- Swift AsyncThrowingStream max()用法及代碼示例
- Swift AsyncThrowingStream dropFirst(_:)用法及代碼示例
- Swift AsyncThrowingStream用法及代碼示例
- Swift AsyncThrowingFlatMapSequence min()用法及代碼示例
- Swift AsyncThrowingMapSequence reduce(_:_:)用法及代碼示例
- Swift AsyncThrowingPrefixWhileSequence drop(while:)用法及代碼示例
- Swift AsyncThrowingMapSequence contains(_:)用法及代碼示例
- Swift AsyncThrowingPrefixWhileSequence filter(_:)用法及代碼示例
- Swift AsyncThrowingDropWhileSequence first(where:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 AsyncThrowingStream filter(_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。