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

last(where:)

Returns the last element of the sequence that satisfies the given predicate. 与えられた述部を満たす、シーケンスの最後の要素を返します。

Declaration 宣言

func last(where predicate: (Element) throws -> Bool) rethrows -> Element?

Parameters パラメータ

predicate

A closure that takes an element of the sequence as its argument and returns a Boolean value indicating whether the element is a match. あるクロージャ、それはシーケンスの1つの要素をそれの引数として取り、その要素が合致するものかどうかを指し示しているブール値を返します。

Return Value 戻り値

The last element of the sequence that satisfies predicate, or nil if there is no element that satisfies predicate. predicate満たすシーケンスの最後の要素、またはpredicateを満たす要素が無いならばnil

Discussion 解説

This example uses the last(where:) method to find the last negative number in an array of integers: この例は、last(where:)メソッドを使って、整数の配列において最後の負数を見つけます:


let numbers = [3, 7, 4, -2, 9, -6, 10, 1]
if let lastNegative = numbers.last(where: { $0 < 0 }) {
    print("The last negative number is \(lastNegative).")
}
// Prints "The last negative number is -6."

Complexity: O(n), where n is the length of the collection. 計算量:O(n)、ここでnはコレクションの長さです。

See Also 参照

Finding Elements 要素を見つける