subscript(_:)
Accesses the element at the specified position.
Declaration
subscript(i: Int) -> Element { get nonmutating set }Parameters
- i:
The position of the element to access.
imust be in the range0..<count.
Overview
The following example uses the buffer pointer’s subscript to access and modify the elements of a mutable buffer pointing to the contiguous contents of an array:
var numbers = [1, 2, 3, 4, 5]
numbers.withUnsafeMutableBufferPointer { buffer in
for i in stride(from: buffer.startIndex, to: buffer.endIndex - 1, by: 2) {
let x = buffer[i]
buffer[i + 1] = buffer[i]
buffer[i] = x
}
}
print(numbers)
// Prints "[2, 1, 4, 3, 5]"
Uninitialized memory cannot be initialized to a nontrivial type
using this subscript. Instead, use an initializing method, such as
`initializeElement(at:to:)`