Array withUnsafeMutableBytes(_:) vs withUnsafeMutableBufferPointer(_:)

I cannot get any clue on the differences between these 2 functions of Array type.

Can anyone explain by examples?

Answered by ssmith_c in 773117022

see the function signatures in the Developer Documentation and follow those to some explanations of the use of the various types of buffers and buffer pointers.

func withUnsafeMutableBytes<R>((UnsafeMutableRawBufferPointer) throws -> R) rethrows -> R

and

func withUnsafeMutableBufferPointer<R>((inout UnsafeMutableBufferPointer<Element>) throws -> R) rethrows -> R

an UnsafeMutableRawBufferPointer is simply a pointer into untyped memory. It is just a bag of bytes. an UnsafeMutableBufferPointer is a pointer into typed memory. Each element is of type Element. It is a bag of Elements.

You may need to use these APIs to interact with C APIs which take pointers to void *, char * or expect a contiguous array of Elements.

Accepted Answer

see the function signatures in the Developer Documentation and follow those to some explanations of the use of the various types of buffers and buffer pointers.

func withUnsafeMutableBytes<R>((UnsafeMutableRawBufferPointer) throws -> R) rethrows -> R

and

func withUnsafeMutableBufferPointer<R>((inout UnsafeMutableBufferPointer<Element>) throws -> R) rethrows -> R

an UnsafeMutableRawBufferPointer is simply a pointer into untyped memory. It is just a bag of bytes. an UnsafeMutableBufferPointer is a pointer into typed memory. Each element is of type Element. It is a bag of Elements.

You may need to use these APIs to interact with C APIs which take pointers to void *, char * or expect a contiguous array of Elements.

Array withUnsafeMutableBytes(_:) vs withUnsafeMutableBufferPointer(_:)
 
 
Q