<!--
{
  "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:from:)-1ol47",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:10Foundation12DataProtocolP9copyBytes2to4fromSiSryqd__G_qd_0_tSXRd_0_5BoundQyd_0_5IndexRtzr0_lF"
  },
  "title" : "copyBytes(to:from:)"
}
-->

# copyBytes(to:from:)

Copies a range of the bytes from the type into a typed memory buffer.

```
@discardableResult func copyBytes<DestinationType, R>(to: UnsafeMutableBufferPointer<DestinationType>, from: R) -> Int where R : RangeExpression, Self.Index == R.Bound
```

## Parameters

`to`

A typed pointer to the 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 typed memory buffer:

```swift
let source: [UInt8] = [0, 1, 2]
var dest: [UInt8] = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]
dest.withUnsafeMutableBufferPointer { typedMemBuffer in
    source.copyBytes(to: typedMemBuffer, from: 1...2)
}
// dest = [0x01, 0x02, 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)