func withUnsafeBytes<R>(_ body: (UnsafeRawBufferPointer) throws -> R) rethrows -> R
Calls a given closure with a raw pointer to the multi array’s storage.
```
let A = try MLMultiArray(shape:[3, 2], dataType: .int32)
A[[1, 2]] = 42
A.withUnsafeBytes { bytes in
let scalarBuffer = bytes.bindMemory(to: Int32.self)
let strideY = A.strides[0].intValue
// Print 42
print("Scalar at (1, 2): \(scalarBuffer[1 * strideY + 2])")
}
```
- Parameter body: A closure with an `UnsafeRawBufferPointer` parameter that points to the
storage for the multi array. If body has a return value, that value is also used as the
return value for the `withUnsafeBytes(_:)` method. The pointer argument is
valid only for the duration of the method's execution.
- Parameter ptr: The pointer to the memory buffer of the multi array.
Calls a given closure with a raw pointer to the multi array’s mutable storage.
Use strides passed to the closure to access the buffer. This is a mutating function, which
may change the underlying buffer layoout.
```
let A = try MLMultiArray(shape:[3, 2], dataType: .int32)
A.withUnsafeMutableBytes { bytes, strides in
let scalarBuffer = bytes.bindMemory(to: Int32.self)
let strideY = strides[0]
scalarBuffer[1 * strideY + 2] = 42 // Set 42 at A[1, 2]
}
```
- Parameter body: A closure with an `UnsafeRawBufferPointer` parameter that points to the
storage for the multi array. If body has a return value, that value is also used as the
return value for the `withUnsafeMutableBytes(_:)` method. The pointer argument is
valid only for the duration of the method's execution.
- Parameter ptr: The pointer to the memory buffer of the multi array.
- Parameter strides: The strides of the buffer in number of scalars, not bytes.
Creates the object with specified strides.
The contents of the object are left uninitialized; the client must initialize it.
- Parameters:
- shape: The shape
- dataType: The data type
- strides: The strides.
init · iOS 13.0+
init
TypeScript
static init(data: Array<number>): MLMultiArray
Swift declaration
convenience init<C>(_ data: C) throws where C : Collection, C.Element == Double
An MLMultiArray constructed with the elements of a collection
whose elements are type Double.
~~~~
let v:[Double] = [3.14159, 2.71828, 1.61803]
let m = try MLMultiArray(v)
print(m)
Double 3 vector
[3.14159,2.71828,1.61803]
~~~~
convenience init<C>(_ data: C) throws where C : Collection, C.Element == Float
An MLMultiArray constructed with the elements of a collection
whose elements are type Float.
~~~~
let v:[Float] = [3.14159, 2.71828, 1.61803]
let m = try MLMultiArray(v)
print(m)
Float32 3 vector
[3.14159,2.71828,1.61803]
~~~~
convenience init<C>(_ data: C) throws where C : Collection, C.Element : FixedWidthInteger
An MLMultiArray constructed with the FixedWidthInteger elements of the collection
converted to Int32.
~~~~
let v:[Int32] = [3, 2, 1]
let m = try MLMultiArray(v)
print(m)
Int32 3 vector
[3,2,1]
~~~~
This initializer will trap if called with data containing FixedWidthInteger elements
that cannot be safely converted to Int32, but it is safe to use with
wider types so long as the actual data is within range.
~~~~
let a = try MLMultiArray([Int.max]) // trap!
let b = try MLMultiArray([Int(Int32.max), Int(Int32.min)]) // This is fine.
~~~~
Skipped declarations
4 declarations were excluded from these bindings. Each reason comes from the generator or the iOS SDK compiler.
MLMultiArray.init(_:)
This declaration needs Swift generic type arguments. (no canonical witness for ShapedArray)