Instance Method インスタンスメソッド

drop(while:)

Returns a sequence by skipping the initial, consecutive elements that satisfy the given predicate.

Declaration 宣言

func drop(while predicate: (Self.Element) throws -> Bool) rethrows -> DropWhileSequence<Self>

Return Value 戻り値

A sequence starting after the initial, consecutive elements that satisfy predicate.

Parameters パラメータ

predicate

A closure that takes an element of the sequence as its argument and returns a Boolean value indicating whether the element should be included in the result.

Discussion 議論

The following example uses the drop(while:) method to skip over the positive numbers at the beginning of the numbers array. The result begins with the first element of numbers that does not satisfy predicate.


let numbers = [3, 7, 4, -2, 9, -6, 10, 1]
let startingWithNegative = numbers.drop(while: { $0 > 0 })
// startingWithNegative == [-2, 9, -6, 10, 1]

If predicate matches every element in the sequence, the result is an empty sequence.