<!--
{
  "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/withUnsafeMutableShapedBufferPointer(using:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Core ML",
      "CoreML"
    ],
    "preciseIdentifier" : "s:6CoreML18MLShapedArraySliceV36withUnsafeMutableShapedBufferPointer5using_qd__AA0cdJ6LayoutO_qd__SryxGz_SaySiGAItKXEtKlF"
  },
  "title" : "withUnsafeMutableShapedBufferPointer(using:_:)"
}
-->

# withUnsafeMutableShapedBufferPointer(using:_:)

Calls the given closure with a pointer to the array’s mutable storage that has a
specified buffer layout.

```
mutating func withUnsafeMutableShapedBufferPointer<R>(using bufferLayout: MLShapedArrayBufferLayout, _ body: (inout UnsafeMutableBufferPointer<Scalar>, [Int], [Int]) throws -> R) rethrows -> R
```

## Discussion

The storage contains the scalars according to buffer layout parameter. For example, the
scalar at `[x1, x2, ... xn]` with strides `[s1, s2, ... sn]` is stored at `ptr[x1 * s1 + x2 * s2 + ... + xn * sn]`

Unlike `withUnsafeMutableShapedBufferPointer(:)`, this function allows the caller to
specify the buffer layout.

```swift
// Initialize the middle 2x2 of 4x4 shaped array using an external data that lays out
// scalars in last-major order.
let data: [Int32] = [1, 3,
                     2, 4]
var array = MLShapedArray<Int32>(repeating: 0, shape: [4, 4])
array[1...2, 1...2].withUnsafeMutableShapedBufferPointer(using: .lastMajorContiguous) { destinationPtr, _, _ in
    data.withUnsafeBufferPointer() { sourcePtr in
        destinationPtr.update(from: sourcePtr)
    }
}

// array.scalars == [0, 0, 0, 0,
//                   0, 1, 2, 0,
//                   0, 3, 4, 0,
//                   0, 0, 0, 0,])
```

---

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)