Calls the given closure with a pointer to the underlying bytes of the array’s mutable contiguous storage.
SDK
- Xcode 8.0+
Framework
- Swift Standard Library
Declaration
Parameters
body
A closure with an
Unsafe
parameter that points to the contiguous storage for the array. If no such storage exists, it is created. IfMutable Raw Buffer Pointer body
has a return value, that value is also used as the return value for thewith
method. The argument is valid only for the duration of the closure’s execution.Unsafe Mutable Bytes(_:)
Return Value
The return value, if any, of the body
closure parameter.
Discussion
The array’s Element
type must be a trivial type, which can be copied with just a bit-for-bit copy without any indirection or reference-counting operations. Generally, native Swift types that do not contain strong or weak references are trivial, as are imported C structs and enums.
The following example copies bytes from the byte
array into numbers
, an array of Int
:
The pointer passed as an argument to body
is valid only for the lifetime of the closure. Do not escape it from the closure 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
.