Generic Function

withUnsafePointer(to:_:)

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

Declaration

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

Parameters

arg

An instance to temporarily use via pointer.

body

A closure that takes a pointer to arg as its sole argument. If the closure has a return value, it is used as the return value of the withUnsafePointer(to:_:) function. The pointer argument is valid only for the duration of the function’s execution.

Return Value

The return value of the body closure, if any.

Discussion

The withUnsafePointer(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 withUnsafePointer(to:_:). Do not store or return the pointer for later use.

See Also

Memory Access

func withUnsafeMutablePointer<T, Result>(to: inout T, (UnsafeMutablePointer<T>) -> Result)

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

func withUnsafeBytes<T, Result>(of: inout T, (UnsafeRawBufferPointer) -> Result)

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

func withUnsafeMutableBytes<T, Result>(of: inout T, (UnsafeMutableRawBufferPointer) -> Result)

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

func swap<T>(inout T, inout T)

Exchange the values of a and b.