---
title: "copyBytes(to:count:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/dataprotocol/copybytes(to:count:)-29t5"
---

# copyBytes(to:count:)

Copies the provided number of bytes from the start of the type into a raw memory buffer.

## Declaration

```swift
@discardableResult func copyBytes(to: UnsafeMutableRawBufferPointer, count: Int) -> Int
```

## Parameters

- `to`: A pointer to the raw memory buffer you want to copy the bytes into.
- `count`: The number of bytes to copy.

## Return Value

Return Value The number of bytes copied.

## Discussion

Discussion The following example copies the number of bytes that count identified from the beginning of 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: 2)     // count == 2 } // dest = [0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF]

## See Also

### Copying Underlying Bytes

- [copyBytes(to:)](foundation/dataprotocol/copybytes(to:)-52wps.md)
- [copyBytes(to:)](foundation/dataprotocol/copybytes(to:)-3mk27.md)
- [copyBytes(to:count:)](foundation/dataprotocol/copybytes(to:count:)-6krsm.md)
- [copyBytes(to:from:)](foundation/dataprotocol/copybytes(to:from:)-1ol47.md)
- [copyBytes(to:from:)](foundation/dataprotocol/copybytes(to:from:)-1y839.md)
