withUnsafeMutablePointer(to:_:)
Calls the given closure with a mutable pointer to the given argument.
Declaration
func withUnsafeMutablePointer<T, E, Result>(to value: inout T, _ body: (UnsafeMutablePointer<T>) throws(E) -> Result) throws(E) -> Result where E : Error, T : ~Copyable, Result : ~CopyableParameters
- value:
An instance to temporarily use via pointer. Note that the
inoutexclusivity rules mean that, like any otherinoutargument,valuecannot be directly accessed by other code for the duration ofbody. Access must only occur through the pointer argument tobodyuntilbodyreturns. - body:
A closure that takes a mutable pointer to
valueas its sole argument. If the closure has a return value, that value is also used as the return value of thewithUnsafeMutablePointer(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.