<!--
{
  "availability" : [
    "iOS: 11.0.0 -",
    "iPadOS: 11.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.13.0 -",
    "tvOS: 11.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 4.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreML",
  "identifier" : "/documentation/CoreML/MLMultiArray/init(shape:dataType:)",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "Core ML",
      "CoreML"
    ],
    "preciseIdentifier" : "c:objc(cs)MLMultiArray(im)initWithShape:dataType:error:"
  },
  "title" : "init(shape:dataType:)"
}
-->

# init(shape:dataType:)

Creates a multidimensional array with a shape and type.

```
init(shape: [NSNumber], dataType: MLMultiArrayDataType) throws
```

## Parameters

`shape`

An integer array that has an element for each dimension in a multiarray that represents its length.

`dataType`

An element type defined by [`MLMultiArrayDataType`](/documentation/CoreML/MLMultiArrayDataType).

## Discussion

This method allocates a contiguous region of memory for the multiarray’s shape. You must set the contents of memory. The multiarray frees the memory in its deinitializer (Swift) or <doc://com.apple.documentation/documentation/ObjectiveC/NSObject-swift.class/dealloc> method (Objective-C).

The following code creates a 3 x 3 multiarray and sets its contents to the value 3.14159.

```swift
// Create a 2D multiarray with dimension 3 x 3.
let shape3x3 = [3, 3] as [NSNumber]

guard let multiarray3x3 = try? MLMultiArray(shape: shape3x3, dataType: .float) else {
    // Handle the error.
    return
}

print("Before: \(multiarray3x3)")

// Initialize the multiarray.
for xCoordinate in 0..<3 {
    for yCoordinate in 0..<3 {
        let key = [xCoordinate, yCoordinate] as [NSNumber]
        multiarray3x3[key] = 3.141_59
    }
}

print("After: \(multiarray3x3)")
```

---

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)