<!--
{
  "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/withUnsafeMutablePointer(to:_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "Swift"
    ],
    "preciseIdentifier" : "s:s24withUnsafeMutablePointer2to_q0_xz_q0_SpyxGq_YKXEtq_YKs5ErrorR_Ri_zRi_0_r1_lF"
  },
  "title" : "withUnsafeMutablePointer(to:_:)"
}
-->

# withUnsafeMutablePointer(to:_:)

Calls the given closure with a mutable pointer to the given argument.

```
func withUnsafeMutablePointer<T, E, Result>(to value: inout T, _ body: (UnsafeMutablePointer<T>) throws(E) -> Result) throws(E) -> Result where E : Error, T : ~Copyable, Result : ~Copyable
```

## Parameters

`value`

An instance to temporarily use via 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 mutable pointer to `value` as its sole
argument. If the closure has a return value, that value is also used
as the return value of the `withUnsafeMutablePointer(to:_:)` function.
The pointer argument is valid only for the duration of the function’s
execution.

## Return Value

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

## Discussion

The `withUnsafeMutablePointer(to:_:)` function is useful for calling
Objective-C APIs that take in/out parameters (and default-constructible
out parameters) by pointer.

The pointer argument to `body` is valid only during the execution of
`withUnsafeMutablePointer(to:_:)`. Do not store or return the pointer for
later use.

---

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)