Subscript access, with an array of indexes, for the multidimensional array.
SDKs
- iOS 11.0+
- macOS 10.13+
- Mac Catalyst 13.0+
- tvOS 11.0+
- watchOS 4.0+
Framework
- Core ML
Declaration
Discussion
Using an array of indexes is the most convenient way to access the elements of the array, but it's also the least efficient. If the subscript is used in a loop, avoid creating a new array for each iteration.
Accessing an element in a three-dimensional array with an array of indexes.
var index: [NSNumber] = [i0, i1, i2]
let value = multiArray[index]
You'll get the best performance through direct access, using the indexes and strides to calculate the offset.
Calculating the offset and directly accessing the element at [i0, i1, i2].
let pointer = UnsafeMutablePointer<Double>(OpaquePointer(multiArray.dataPointer))
let offset = i0 * multiArray.strides[0].intValue +
i1 * multiArray.strides[1].intValue +
i2 * multiArray.strides[2].intValue
let value = pointer[offset]