FunctionSwift

withUnsafePointer(to:_:)

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

func withUnsafePointer<T, Result>(to value: inout T, _ body: (UnsafePointer<T>) throws -> Result) rethrows -> Result

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 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.

Returns

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

Overview

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.