<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Swift/withUnsafeMutableBytes(of:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:s22withUnsafeMutableBytes2of_q0_xz_q0_Swq_YKXEtq_YKs5ErrorR_Ri_zRi0_zRi_0_r1_lF"
  },
  "title" : "withUnsafeMutableBytes(of:_:)"
}
-->

# withUnsafeMutableBytes(of:_:)

Invokes the given closure with a mutable buffer pointer covering the raw
bytes of the given argument.

```
func withUnsafeMutableBytes<T, E, Result>(of value: inout T, _ body: (UnsafeMutableRawBufferPointer) throws(E) -> Result) throws(E) -> Result where E : Error, T : ~Copyable, T : ~Escapable, Result : ~Copyable
```

## Parameters

`value`

An instance to temporarily access through a mutable raw buffer
pointer.
Note that the `inout` exclusivity rules mean that, like any other
`inout` argument, `value` cannot be directly accessed by other code
for the duration of `body`. Access must only occur through the pointer
argument to `body` until `body` returns.

`body`

A closure that takes a raw buffer pointer to the bytes of `value`
as its sole argument. If the closure has a return value, that value is
also used as the return value of the `withUnsafeMutableBytes(of:_:)`
function. The buffer pointer argument is valid only for the duration
of the closure’s execution.

## Return Value

The return value, if any, of the `body` closure.

## Discussion

The buffer pointer argument to the `body` closure provides a collection
interface to the raw bytes of `value`. The buffer is the size of the
instance passed as `value` and does not include any remote storage.

---

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)