<!--
{
  "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/Data/replaceSubrange(_:with:)-9nzh",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:10Foundation4DataV15replaceSubrange_4withySnySiG_SRyxGtlF"
  },
  "title" : "replaceSubrange(_:with:)"
}
-->

# replaceSubrange(_:with:)

Replaces a region of bytes in the data with new bytes from a buffer.

```
mutating func replaceSubrange<SourceType>(_ subrange: Range<Data.Index>, with buffer: UnsafeBufferPointer<SourceType>)
```

## Parameters

`subrange`

The range in the data to replace.

`buffer`

The replacement bytes.

## Discussion

This will resize the data if required, to fit the entire contents of `buffer`.

Precondition: The bounds of `subrange` must be valid indices of the collection.

---

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)