Instance Property

last

The last element of the collection.

Declaration

var last: Bound? { get }
Available when Bound conforms to Strideable and Bound.Stride conforms to SignedInteger.

Discussion

If the collection is empty, the value of this property is nil.

let numbers = [10, 20, 30, 40, 50]
if let lastNumber = numbers.last {
    print(lastNumber)
}
// Prints "50"

Complexity: O(1)

See Also

Accessing Elements

var first: Bound?

The first element of the collection.

subscript(ClosedRange<Bound>.Index) -> Bound

Accesses the element at specified position.

subscript<R>(R) -> Slice<ClosedRange<Bound>>

Accesses the contiguous subrange of the collection’s elements specified by a range expression.

subscript((UnboundedRange_) -> ()) -> Slice<ClosedRange<Bound>>
subscript(Range<ClosedRange<Bound>.Index>) -> Slice<ClosedRange<Bound>>

Accesses a contiguous subrange of the collection’s elements.

func randomElement() -> Bound?

Returns a random element of the collection.

func randomElement<T>(using: inout T) -> Bound?

Returns a random element of the collection, using the given generator as a source for randomness.