實例方法
prefix(while:)
返回包含滿足給定謂詞的初始連續元素的序列。
聲明
func prefix(while predicate: (Self.Element) throws -> Bool) rethrows -> [Self.Element]
返回值
滿足 predicate
的初始連續元素的序列。
參數
predicate
一個閉包,它將序列的一個元素作為其參數,並返回一個布爾值,指示該元素是否應包含在結果中。
詳述
以下示例使用 prefix(while:)
方法在 numbers
數組的開頭查找正數。 numbers
的每個元素直到但不包括第一個負值都包含在結果中。
let numbers = [3, 7, 4, -2, 9, -6, 10, 1]
let positivePrefix = numbers.prefix(while: { $0 > 0 })
// positivePrefix == [3, 7, 4]
如果predicate
匹配序列中的每個元素,則生成的序列包含序列的每個元素。
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相關用法
- Swift UnsafeRawBufferPointer.Iterator prefix(_:)用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator dropFirst(_:)用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator max()用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator filter(_:)用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator drop(while:)用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator min()用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator contains(where:)用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator contains(_:)用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator reduce(into:_:)用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator map(_:)用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator sorted(by:)用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator compactMap(_:)用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator enumerated()用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator first(where:)用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator min(by:)用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator forEach(_:)用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator max(by:)用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator allSatisfy(_:)用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator split(maxSplits:omittingEmptySubsequences:whereSeparator:)用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator split(separator:maxSplits:omittingEmptySubsequences:)用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator shuffled()用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator suffix(_:)用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator lexicographicallyPrecedes(_:)用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator elementsEqual(_:)用法及代碼示例
- Swift UnsafeRawBufferPointer.Iterator starts(with:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 UnsafeRawBufferPointer.Iterator prefix(while:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。