Instance Methodswift 6.0.1Swift
lastIndex(of:)
Returns the last index where the specified value appears in the collection.
func lastIndex(of element: Self.Element) -> Self.Index?
Parameters
- element
An element to search for in the collection.
Returns
The last index where element
is found. If element
is not found in the collection, this method returns nil
.
After using lastIndex(of:)
to find the position of the last instance of a particular element in a collection, you can use it to access the element by subscripting. This example shows how you can modify one of the names in an array of students.
var students = ["Ben", "Ivy", "Jordell", "Ben", "Maxime"]
if let i = students.lastIndex(of: "Ben") {
students[i] = "Benjamin"
}
print(students)
// Prints "["Ben", "Ivy", "Jordell", "Benjamin", "Max"]"
Other members in extension
Instance members
func difference<C>(from: C
) -> CollectionDifference<Self.Element> Returns the difference needed to produce this collection’s ordered elements from the given collection.