---
title: "copyBytes(to:from:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/data/copybytes(to:from:)-4o6zj"
---

# copyBytes(to:from:)

Copies the bytes in a range from the data into a buffer.

## Declaration

```swift
func copyBytes<DestinationType>(to buffer: UnsafeMutableBufferPointer<DestinationType>, from range: Range<Data.Index>? = nil) -> Int
```

## Parameters

- `buffer`: A buffer to copy the data into.
- `range`: A range in the data to copy into the buffer. If the range is empty, this function will return 0 without copying anything. If the range is nil, as much data as will fit into buffer is copied.

## Return Value

Return Value Number of bytes copied into the destination buffer.

## Discussion

Discussion If the count of the range is greater than MemoryLayout<DestinationType>.stride * buffer.count then only the first N bytes will be copied into the buffer.Precondition: The range must be within the bounds of the data. Otherwise fatalError is called.

## See Also

### Accessing Underlying Memory

- [withUnsafeBytes(_:)](foundation/data/withunsafebytes(_:).md)
- [withUnsafeMutableBytes(_:)](foundation/data/withunsafemutablebytes(_:)-7ac1g.md)
- [copyBytes(to:count:)](foundation/data/copybytes(to:count:).md)
- [copyBytes(to:from:)](foundation/data/copybytes(to:from:)-8qk4r.md)
