Invokes the given closure with a pointer to the given argument.
SDK
- Xcode 10.0+
Framework
- Swift Standard Library
Declaration
func withUnsafePointer<T, Result>(to value: T, _ body: (UnsafePointer <T>) throws -> Result) rethrows -> Result
Parameters
valueAn instance to temporarily use via pointer.
bodyA closure that takes a pointer to
valueas its sole argument. If the closure has a return value, that value is also used as the return value of thewithfunction. 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 toUnsafe Pointer(to: _:) Unsafeor any other mutable pointer type. If you need to mutate the argument through the pointer, useMutable Pointer withinstead.Unsafe Mutable Pointer(to: _:)
Return Value
The return value, if any, of the body closure.
Discussion
The with 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 with. Do not store or return the pointer for later use.