<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "macOS: 12.0.0 -",
    "tvOS: 15.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 8.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreML",
  "identifier" : "/documentation/CoreML/MLShapedArrayProtocol",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "Core ML"
    ],
    "preciseIdentifier" : "s:6CoreML21MLShapedArrayProtocolP"
  },
  "title" : "MLShapedArrayProtocol"
}
-->

# MLShapedArrayProtocol

An interface that defines a shaped array type.

```
protocol MLShapedArrayProtocol<Scalar> : ExpressibleByArrayLiteral, MutableCollection, RandomAccessCollection where Self.Index == Int
```

## Topics

### Creating a shaped array type

[`init<S>(scalars: S, shape: [Int])`](/documentation/CoreML/MLShapedArrayProtocol/init(scalars:shape:))

Creates a shaped array type from an array of values.

[`init(repeating: Self.Scalar, shape: [Int])`](/documentation/CoreML/MLShapedArrayProtocol/init(repeating:shape:))

Creates a shaped array type that initializes every element to the same value.

[`init(identityMatrixOfSize:)`](/documentation/CoreML/MLShapedArrayProtocol/init(identityMatrixOfSize:))

Initialize as an identity matrix.

[`init(randomScalarsIn:shape:)`](/documentation/CoreML/MLShapedArrayProtocol/init(randomScalarsIn:shape:))

Initialize the shaped array with random scalar values.

[`init(bytesNoCopy: UnsafeRawPointer, shape: [Int], deallocator: Data.Deallocator)`](/documentation/CoreML/MLShapedArrayProtocol/init(bytesNoCopy:shape:deallocator:))

Creates a shaped array type from a data pointer.

[`init(bytesNoCopy: UnsafeRawPointer, shape: [Int], strides: [Int], deallocator: Data.Deallocator)`](/documentation/CoreML/MLShapedArrayProtocol/init(bytesNoCopy:shape:strides:deallocator:))

Creates a shaped array type from a data pointer with memory strides.

[`init(unsafeUninitializedShape: [Int], initializingWith: (inout UnsafeMutableBufferPointer<Self.Scalar>, [Int]) throws -> Void) rethrows`](/documentation/CoreML/MLShapedArrayProtocol/init(unsafeUninitializedShape:initializingWith:))

Creates a shaped array type from a shape and a closure that initializes its memory.

### Creating a shaped array type from another type

[`init(MLMultiArray)`](/documentation/CoreML/MLShapedArrayProtocol/init(_:))

Creates a shaped array type from a multiarray.

[`init(converting:)`](/documentation/CoreML/MLShapedArrayProtocol/init(converting:))

Initialize by converting a MLMultiArray of different scalar type.

### Inspecting a shaped array type

[`var shape: [Int]`](/documentation/CoreML/MLShapedArrayProtocol/shape)

An integer array in which each element represents the size of the corresponding dimension.

[`var strides: [Int]`](/documentation/CoreML/MLShapedArrayProtocol/strides)

An integer array in which each element is the number of memory locations that spans the length of the corresponding dimension.

[`var count: Int`](/documentation/CoreML/MLShapedArrayProtocol/count)

The number of elements in the shaped array’s first dimension.

[`var isScalar: Bool`](/documentation/CoreML/MLShapedArrayProtocol/isScalar)

A Boolean value that indicates whether the shaped array lacks a shape.

[`var scalarCount: Int`](/documentation/CoreML/MLShapedArrayProtocol/scalarCount)

The total number of elements in the shaped array type.

[`var scalar: Self.Scalar?`](/documentation/CoreML/MLShapedArrayProtocol/scalar-swift.property)

A computed property that returns the first element when the shape isn’t empty, or sets the shaped array’s underlying scalar type.

[`var scalars: [Self.Scalar]`](/documentation/CoreML/MLShapedArrayProtocol/scalars)

A computed property that generates a linear array that contains every element, or assigns the elements of an array to the shaped array’s elements.

[`func withUnsafeShapedBufferPointer<R>((UnsafeBufferPointer<Self.Scalar>, [Int], [Int]) throws -> R) rethrows -> R`](/documentation/CoreML/MLShapedArrayProtocol/withUnsafeShapedBufferPointer(_:))

Provides read-only access of the shaped array’s underlying memory to a closure.

### Accessing elements

[`subscript<C>(scalarAt _: C) -> Self.Scalar`](/documentation/CoreML/MLShapedArrayProtocol/subscript(scalarAt:))

Accesses an element and a multidimensional location.

[`subscript(_:)`](/documentation/CoreML/MLShapedArrayProtocol/subscript(_:))

A slice of the shaped array for the selected leading axes.

### Modifying a shaped array type

[`func fill(with:)`](/documentation/CoreML/MLShapedArrayProtocol/fill(with:))

Fills the array with a value.

[`func withUnsafeMutableShapedBufferPointer<R>((inout UnsafeMutableBufferPointer<Self.Scalar>, [Int], [Int]) throws -> R) rethrows -> R`](/documentation/CoreML/MLShapedArrayProtocol/withUnsafeMutableShapedBufferPointer(_:))

Provides read-write access of the shaped array’s underlying memory to a closure.

### Supporting types

[`associatedtype Scalar : MLShapedArrayScalar`](/documentation/CoreML/MLShapedArrayProtocol/Scalar-swift.associatedtype)

Represents the underlying scalar type of the shaped array type.

[`struct MLShapedArraySlice`](/documentation/CoreML/MLShapedArraySlice)

A multidimensional subset of elements from a shaped array type.

[`protocol MLShapedArrayScalar`](/documentation/CoreML/MLShapedArrayScalar)

A type that associates a scalar with a shaped array.

[`protocol MLShapedArrayRangeExpression`](/documentation/CoreML/MLShapedArrayRangeExpression)

An interface for a range expression, which you typically use with subscripts of shaped array types.

## Relationships

### Conforming Types

[`MLShapedArray`](/documentation/CoreML/MLShapedArray)

[`MLShapedArraySlice`](/documentation/CoreML/MLShapedArraySlice)

### Inherits From

[`Collection`](/documentation/Swift/Collection)

[`ExpressibleByArrayLiteral`](/documentation/Swift/ExpressibleByArrayLiteral)

[`MutableCollection`](/documentation/Swift/MutableCollection)

[`Sequence`](/documentation/Swift/Sequence)

[`RandomAccessCollection`](/documentation/Swift/RandomAccessCollection)

[`BidirectionalCollection`](/documentation/Swift/BidirectionalCollection)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)