<!--
{
  "availability" : [
    "iOS: 18.0.0 -",
    "iPadOS: 18.0.0 -",
    "macCatalyst: 18.0.0 -",
    "macOS: 15.0.0 -",
    "tvOS: 18.0.0 -",
    "visionOS: 2.0.0 -",
    "watchOS: 11.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreML",
  "identifier" : "/documentation/CoreML/MLShapedArray/changingLayout(to:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Core ML",
      "CoreML"
    ],
    "preciseIdentifier" : "s:6CoreML13MLShapedArrayV14changingLayout2toACyxGAA0cd6BufferF0O_tF"
  },
  "title" : "changingLayout(to:)"
}
-->

# changingLayout(to:)

Returns a copy with the specified buffer layout.

```
func changingLayout(to bufferLayout: MLShapedArrayBufferLayout) -> MLShapedArray<Scalar>
```

## Parameters

`bufferLayout`

The desired buffer layout.

## Discussion

The returned shaped array will have `.strides` property according to the requested
layout.

The function may return a heap-memory backed shaped array even if `self` is backed by a
pixel buffer.

```swift
let source = MLShapedArray<Int32>(scalars: 0..., shape: [2, 2])

// Returns a new MLShapedArray with the specified strides.
_ = source.changingLayout(to: .custom(strides: [4, 1]))

// Returns a new MLShapedArray with the first-major contiguous layout.
_ = source.changingLayout(to: .firstMajorContiguous)

// Returns a new MLShapedArray with the last-major contiguous layout.
_ = source.changingLayout(to: .lastMajorContiguous)
```

The `withUnsafeShapedBufferPointer` function provides read-only access to the underlying
buffer of the layout.

The `withUnsafeMutableShapedBufferPointer(body:)` function may provide a buffer of
different layout due to copy-on-write. Use
`withUnsafeMutableShapedBufferPointer(bufferLayout:body:)` if you need a specific buffer
layout.

It raises a precondition error if the custom strides and the shape have different ranks.

---

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)