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

# withUnsafePointer(to:_:)

Invokes the given closure with a pointer to the given argument.

```
func withUnsafePointer<T, E, Result>(to value: borrowing T, _ body: (UnsafePointer<T>) throws(E) -> Result) throws(E) -> Result where E : Error, T : ~Copyable, Result : ~Copyable
```

## Parameters

`value`

An instance to temporarily use via pointer.

`body`

A closure that takes a 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 `withUnsafePointer(to:_:)` function. The pointer argument
is valid only for the duration of the function’s execution.
It is undefined behavior to try to mutate through the pointer argument
by converting it to `UnsafeMutablePointer` or any other mutable pointer
type. If you need to mutate the argument through the pointer, use
`withUnsafeMutablePointer(to:_:)` instead.

## Return Value

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

## Discussion

The `withUnsafePointer(to:_:)` function is useful for calling Objective-C
APIs that take in parameters by const pointer.

The pointer argument to `body` is valid only during the execution of
`withUnsafePointer(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)