subscript(_:)
Accesses the element at the specified position.
Declaration
subscript(i: Int) -> Element { get }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 every other element of the buffer:
let numbers = [1, 2, 3, 4, 5]
let sum = numbers.withUnsafeBufferPointer { buffer -> Int in
var result = 0
for i in stride(from: buffer.startIndex, to: buffer.endIndex, by: 2) {
result += buffer[i]
}
return result
}
// 'sum' == 9