<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 8.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/DataProtocol/copyBytes(to:count:)-29t5",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:10Foundation12DataProtocolP9copyBytes2to5countSiSw_SitF"
  },
  "title" : "copyBytes(to:count:)"
}
-->

# copyBytes(to:count:)

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

```
@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

The number of bytes copied.

## 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:

```swift
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]
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)