Calls the given closure with a pointer to the array’s mutable contiguous storage.
SDK
- Xcode 10.0+
Framework
- Swift Standard Library
Declaration
Parameters
body
A closure with an
Unsafe
parameter that points to the contiguous storage for the array. IfMutable Buffer Pointer body
has a return value, that value is also used as the return value for thewith
method. The pointer argument is valid only for the duration of the method’s execution.Unsafe Mutable Buffer Pointer(_:)
Return Value
The return value, if any, of the body
closure parameter.
Discussion
Often, the optimizer can eliminate bounds checks within an array algorithm, but when that fails, invoking the same algorithm on the buffer pointer passed into your closure lets you trade safety for speed.
The following example shows how modifying the contents of the Unsafe
argument to body
alters the contents of the array:
The pointer passed as an argument to body
is valid only during the execution of with
. Do not store or return the pointer for later use.
Warning
Do not rely on anything about the array that is the target of this method during execution of the body
closure; it might not appear to have its correct value. Instead, use only the Unsafe
argument to body
.