Skip to content

Instance MethodFoundationEssentials

copyBytes(to:)

iOS 8.0+ iPadOS 8.0+ Mac Catalyst 8.0+ macOS 10.10+ tvOS 9.0+ watchOS 2.0+
Copies the bytes of data from the type into a raw memory buffer.
@discardableResult func copyBytes(to ptr: UnsafeMutableRawBufferPointer) -> Int

Parameters

ptr

A pointer to the raw memory buffer you want to copy the bytes into.

Return Value

The number of bytes copied.

Discussion

The following example copies the bytes from the raw memory buffer into the provided raw memory buffer:

let source: [UInt8] = [0, 1, 2]
var dest: [UInt8] = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
dest.withUnsafeMutableBytes { destBufferPtr in
    let count = source.copyBytes(to: destBufferPtr)
    // count == 3
}
// dest = [0x00, 0x01, 0x02, 0xFF, 0xFF, 0xFF]