copyBytes(to:from:)
Copies a range of the bytes from the type into a raw memory buffer.
Declaration
@discardableResult func copyBytes<R>(to: UnsafeMutableRawBufferPointer, from: R) -> Int where R : RangeExpression, Self.Index == R.BoundParameters
- to:
A pointer to the raw memory buffer you want to copy the bytes into.
- from:
The range of bytes to copy.
Return Value
The number of bytes copied.
Discussion
The following example copies the source bytes that the provided range identifies into the beginning of the specified raw memory buffer:
let source: [UInt8] = [0, 1, 2]
var dest: [UInt8] = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
_ = dest.withUnsafeMutableBytes { destBufferPtr in
source.copyBytes(to: destBufferPtr, from: 1...2)
}
// dest = [0x01, 0x02, 0xFF, 0xFF, 0xFF, 0xFF]