<!--
{
  "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.

```objc
NSError *error = nil;

// Create a 2D multiarray with dimension 3 x 3.
NSArray<NSNumber *> *shape3x3 = @[@3, @3];

MLMultiArray *multiarray3x3 = [[MLMultiArray alloc] initWithShape:shape3x3 dataType:MLMultiArrayDataTypeFloat error: &error];
if (error != nil) {
    // Handle the error.
    return;
}

NSLog(@"Before: %@\n", multiarray3x3);

// Initialize the multiarray.
for (int x = 0; x < 3; x++) {
    for (int y = 0; y < 3; y++) {
        NSNumber *xSubscript = [NSNumber numberWithInt:x];
        NSNumber *ySubscript = [NSNumber numberWithInt:y];

        [multiarray3x3 setObject:@3.14159
               forKeyedSubscript:@[xSubscript, ySubscript]];
    }
}

NSLog(@"After: %@\n", 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)