Required Instance Subscriptswift 6.0.1Swift
subscript(_:)
Accesses the element at the specified position.
override subscript(position: Self.Index) -> Self.Element { get set }
Parameters
- position
The position of the element to access.
position
must be a valid index of the collection that is not equal to theendIndex
property.
For example, you can replace an element of an array by using its subscript.
var streets = ["Adams", "Bryant", "Channing", "Douglas", "Evarts"]
streets[1] = "Butler"
print(streets[1])
// Prints "Butler"
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.
Restates
subscript(Self.Index
) -> Self.Element Accesses the element at the specified position.
Other requirements
Type members
Instance members
subscript(Range<Self.Index>
) -> Self.SubSequence Accesses a contiguous subrange of the collection’s elements.
func partition(by: (Self.Element) throws -> Bool
) rethrows -> Self.Index Reorders the elements of the collection such that all the elements that match the given predicate are after all the elements that don’t match.
func swapAt(Self.Index, Self.Index
) Exchanges the values at the specified indices of the collection.
func withContiguousMutableStorageIfAvailable<R>((inout UnsafeMutableBufferPointer<Self.Element>) throws -> R
) rethrows -> R? Executes a closure on the collection’s contiguous storage.
Citizens in Swift
Default implementations
subscript<R>(R
) -> Self.SubSequence subscript((UnboundedRange_) -> ()
) -> Self.SubSequence subscript(Range<Self.Index>
) -> Slice<Self> Accesses a contiguous subrange of the collection’s elements.