---
title: "replaceBytes(in:withBytes:)"
framework: foundation
role: symbol
role_heading: Instance Method
path: "foundation/nsmutabledata/replacebytes(in:withbytes:)"
---

# replaceBytes(in:withBytes:)

Replaces with a given set of bytes a given range within the contents of the receiver.

## Declaration

```swift
func replaceBytes(in range: NSRange, withBytes bytes: UnsafeRawPointer)
```

## Parameters

- `range`: The range within the receiver’s contents to replace with bytes. The range must not exceed the bounds of the receiver.
- `bytes`: The data to insert into the receiver’s contents.

## Discussion

Discussion If the location of range isn’t within the receiver’s range of bytes, an NSRangeException is raised. The receiver is resized to accommodate the new bytes, if necessary. A sample using this method is given in Working With Mutable Binary Data.

## See Also

### Modifying Bytes

- [replaceBytes(in:withBytes:length:)](foundation/nsmutabledata/replacebytes(in:withbytes:length:).md)
- [resetBytes(in:)](foundation/nsmutabledata/resetbytes(in:).md)
- [setData(_:)](foundation/nsmutabledata/setdata(_:).md)
