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

# changingLayout(to:)

Returns a copy with the specified buffer layout.

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

## Parameters

`bufferLayout`

The desired buffer layout.

## Discussion

The returned shaped array slice 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: [4, 4])
let slice = source[1...2, 1...2] // slice.shape == [2, 2]

// Returns a new MLShapedArraySlice with the specified strides.
_ = slice.changingLayout(to: .custom(strides: [3, 1]))

// Returns a new MLShapedArraySlice with the first-major contiguous layout.
_ = source.changingLayout(to: .firstMajorContiguous) // strides = [2, 1]

// Returns a new MLShapedArraySlice with the last-major contiguous layout.
_ = source.changingLayout(to: .lastMajorContiguous) // strides = [1, 2]
```

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)