Subscript

subscript(_:)

Accesses the element at specified position.

Declaration

subscript(position: Range<Bound>.Index) -> Range<Bound>.Element { get }
Available when Bound conforms to Strideable and Bound.Stride conforms to SignedInteger.

Parameters

position

The position of the element to access. position must be a valid index of the range, and must not equal the range’s end index.

Discussion

You can subscript a collection with any valid index other than the collection’s end index. The end index refers to the position one past the last element of a collection, so it doesn’t correspond with an element.

See Also

Accessing Elements

var first: Bound?

The first element of the collection.

var last: Bound?

The last element of the collection.

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

Accesses the subsequence bounded by the given range.

subscript<R>(R) -> Range<Bound>

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

subscript((UnboundedRange_) -> ()) -> Range<Bound>
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.