Invokes the given closure with a buffer pointer covering the raw bytes of the given argument.
SDK
- Xcode 8.0+
Framework
- Swift Standard Library
Declaration
func withUnsafeBytes<T, Result>(of value: inout T, _ body: (Unsafe Raw Buffer Pointer) throws -> Result) rethrows -> Result
Parameters
valueAn instance to temporarily access through a raw buffer 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.bodyA closure that takes a raw buffer pointer to the bytes of
valueas its sole argument. If the closure has a return value, that value is also used as the return value of thewithfunction. The buffer pointer argument is valid only for the duration of the closure’s execution. It is undefined behavior to attempt to mutate through the pointer by conversion toUnsafe Bytes(of: _:) Unsafeor any other mutable pointer type. If you want to mutate a value by writing through a pointer, useMutable Raw Buffer Pointer withinstead.Unsafe Mutable Bytes(of: _:)
Return Value
The return value, if any, of the body closure.
Discussion
The buffer pointer argument to the body closure provides a collection interface to the raw bytes of value. The buffer is the size of the instance passed as value and does not include any remote storage.