實例方法
first(where:)
返回滿足給定謂詞的序列的第一個元素。
聲明
func first(where predicate: (Self.Element) async throws -> Bool) async rethrows -> Self.Element?
返回值
滿足 predicate
的序列的第一個元素,如果沒有滿足 predicate
的元素,則為 nil
。
參數
predicate
一個閉包,它將異步序列的元素作為其參數並返回一個布爾值,該值指示該元素是否匹配。
詳述
在此示例中,名為 Counter
的異步序列生成從 1
到 10
的 Int
值。 first(where:)
方法返回可以被 2
和 3
整除的序列的第一個成員。
let divisibleBy2And3 = await Counter(howHigh: 10)
.first { $0 % 2 == 0 && $0 % 3 == 0 }
print(divisibleBy2And3 ?? "none")
// Prints: 6
每次異步序列產生一個元素時,謂詞都會執行,直到謂詞找到匹配項或序列結束。
可用版本
iOS 13.0+, iPadOS 13.0+, macOS 10.15+, Mac Catalyst 13.0+, tvOS 13.0+, watchOS 6.0+
相關用法
- Swift AsyncFlatMapSequence filter(_:)用法及代碼示例
- Swift AsyncFlatMapSequence flatMap(_:)用法及代碼示例
- Swift AsyncFlatMapSequence min()用法及代碼示例
- Swift AsyncFlatMapSequence map(_:)用法及代碼示例
- Swift AsyncFlatMapSequence compactMap(_:)用法及代碼示例
- Swift AsyncFlatMapSequence max(by:)用法及代碼示例
- Swift AsyncFlatMapSequence contains(_:)用法及代碼示例
- Swift AsyncFlatMapSequence reduce(_:_:)用法及代碼示例
- Swift AsyncFlatMapSequence max()用法及代碼示例
- Swift AsyncFlatMapSequence drop(while:)用法及代碼示例
- Swift AsyncFlatMapSequence min(by:)用法及代碼示例
- Swift AsyncFlatMapSequence contains(where:)用法及代碼示例
- Swift AsyncFlatMapSequence prefix(_:)用法及代碼示例
- Swift AsyncFlatMapSequence prefix(while:)用法及代碼示例
- Swift AsyncFlatMapSequence allSatisfy(_:)用法及代碼示例
- Swift AsyncFlatMapSequence dropFirst(_:)用法及代碼示例
- Swift AsyncFilterSequence reduce(_:_:)用法及代碼示例
- Swift AsyncFilterSequence map(_:)用法及代碼示例
- Swift AsyncFilterSequence flatMap(_:)用法及代碼示例
- Swift AsyncFilterSequence compactMap(_:)用法及代碼示例
- Swift AsyncFilterSequence allSatisfy(_:)用法及代碼示例
- Swift AsyncFilterSequence first(where:)用法及代碼示例
- Swift AsyncFilterSequence min()用法及代碼示例
- Swift AsyncFilterSequence max()用法及代碼示例
- Swift AsyncFilterSequence filter(_:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 AsyncFlatMapSequence first(where:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。