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

lastIndex(where:)

Returns the index of the last element in the collection that matches the given predicate. 与えられた述部に合致する、コレクションの中の最後の要素のインデックスを返します。

Declaration 宣言

func lastIndex(where predicate: (Base.Element) throws -> Bool) rethrows -> Index?

Parameters パラメータ

predicate

A closure that takes an element as its argument and returns a Boolean value that indicates whether the passed element represents a match. あるクロージャ、それは1つの要素をそれの引数として取り、渡された要素が合致するものであるかどうかを指し示すブール値を返します。

Return Value 戻り値

The index of the last element in the collection that matches predicate, or nil if no elements match. predicateに合致する、コレクションの中の最後の要素のインデックス、または合致する要素がないならばnil

Discussion 解説

You can use the predicate to find an element of a type that doesn’t conform to the Equatable protocol or to find an element that matches particular criteria. This example finds the index of the last name that begins with the letter A: あなたは述部を使って、Equatableプロトコルに準拠しない型の要素を見つける、または特定の基準に合致する要素を見つけることができます。この例は、文字A:で始まる、最後の名前のインデックスを見つけます:


let students = ["Kofi", "Abena", "Peter", "Kweku", "Akosua"]
if let i = students.lastIndex(where: { $0.hasPrefix("A") }) {
    print("\(students[i]) starts with 'A'!")
}
// Prints "Akosua starts with 'A'!"

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