<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSMutableData",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSMutableData"
  },
  "title" : "NSMutableData"
}
-->

# NSMutableData

An object representing a dynamic byte buffer in memory.

```
class NSMutableData
```

## Overview

In Swift, this object bridges to [`Data`](/documentation/Foundation/Data); use [`NSMutableData`](/documentation/Foundation/NSMutableData) when you need reference semantics or other Foundation-specific behavior.

`NSMutableData` and its superclass `NSData` provide data objects, or object-oriented wrappers for byte buffers. Data objects let simple allocated buffers (that is, data with no embedded pointers) take on the behavior of Foundation objects. They are typically used for data storage and are also useful in Distributed Objects applications, where data contained in data objects can be copied or moved between applications. `NSData` creates static data objects, and `NSMutableData` creates dynamic data objects. You can easily convert one type of data object to the other with the initializer that takes an `NSData` object or an  `NSMutableData` object as an argument.

The following [`NSData`](/documentation/Foundation/NSData) methods change when used on a mutable data object:

- [`init(bytesNoCopy:length:freeWhenDone:)`](/documentation/Foundation/NSData/init(bytesNoCopy:length:freeWhenDone:))
- [`init(bytesNoCopy:length:deallocator:)`](/documentation/Foundation/NSData/init(bytesNoCopy:length:deallocator:))
- [`init(bytesNoCopy:length:)`](/documentation/Foundation/NSData/init(bytesNoCopy:length:))
- [`dataWithBytesNoCopy:length:freeWhenDone:`](/documentation/Foundation/NSData/dataWithBytesNoCopy:length:freeWhenDone:)
- [`dataWithBytesNoCopy:length:`](/documentation/Foundation/NSData/dataWithBytesNoCopy:length:)

When called, the bytes are immediately copied and then the buffer is freed.

`NSMutableData` is “toll-free bridged” with its Core Foundation counterpart, <doc://com.apple.documentation/documentation/CoreFoundation/CFData>. See [Toll-Free Bridging](https://developer.apple.com/library/archive/documentation/General/Conceptual/CocoaEncyclopedia/Toll-FreeBridgin/Toll-FreeBridgin.html#//apple_ref/doc/uid/TP40010810-CH2) for more information on toll-free bridging.

> Important:
> The Swift overlay to the Foundation framework provides the ``doc://com.apple.foundation/documentation/Foundation/Data`` structure, which bridges to the ``doc://com.apple.foundation/documentation/Foundation/NSMutableData`` class and its immutable superclass ``doc://com.apple.foundation/documentation/Foundation/NSData``. For more information about value types, see <doc://com.apple.documentation/documentation/Swift/working-with-foundation-types>.

## Topics

### Creating Mutable Data

[`dataWithCapacity:`](/documentation/Foundation/NSMutableData/dataWithCapacity:)

Creates and returns a mutable data object capable of holding the specified number of bytes.

[`dataWithLength:`](/documentation/Foundation/NSMutableData/dataWithLength:)

Creates and returns an mutable data object containing a given number of zeroed bytes.

[`init(capacity:)`](/documentation/Foundation/NSMutableData/init(capacity:))

Returns an initialized mutable data object capable of holding the specified number of bytes.

[`init(length:)`](/documentation/Foundation/NSMutableData/init(length:))

Initializes and returns a mutable data object containing a given number of zeroed bytes.

### Accessing Raw Bytes

[`mutableBytes`](/documentation/Foundation/NSMutableData/mutableBytes)

A pointer to the data contained by the mutable data object.

### Counting Bytes

[`length`](/documentation/Foundation/NSMutableData/length)

The number of bytes contained in the mutable data object.

### Adding Bytes

[`append(_:length:)`](/documentation/Foundation/NSMutableData/append(_:length:))

Appends to the receiver a given number of bytes from a given buffer.

[`append(_:)`](/documentation/Foundation/NSMutableData/append(_:))

Appends the content of another data object to the receiver.

[`increaseLength(by:)`](/documentation/Foundation/NSMutableData/increaseLength(by:))

Increases the length of the receiver by a given number of bytes.

### Modifying Bytes

[`replaceBytes(in:withBytes:)`](/documentation/Foundation/NSMutableData/replaceBytes(in:withBytes:))

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

[`replaceBytes(in:withBytes:length:)`](/documentation/Foundation/NSMutableData/replaceBytes(in:withBytes:length:))

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

[`resetBytes(in:)`](/documentation/Foundation/NSMutableData/resetBytes(in:))

Replaces with zeroes the contents of the receiver in a given range.

[`setData(_:)`](/documentation/Foundation/NSMutableData/setData(_:))

Replaces the entire contents of the receiver with the contents of another data object.

### Compressing and Decompressing Data

[`compress(using:)`](/documentation/Foundation/NSMutableData/compress(using:))

Compresses the data object’s bytes using an algorithm that you specify.

[`decompress(using:)`](/documentation/Foundation/NSMutableData/decompress(using:))

Decompresses the data object’s bytes.

[`NSData.CompressionAlgorithm`](/documentation/Foundation/NSData/CompressionAlgorithm)

An algorithm that indicates how to compress or decompress data.

[`NSCompressionErrorMaximum`](/documentation/Foundation/NSCompressionErrorMaximum-swift.var)

The end of the range of error codes reserved for compression errors.

[`NSCompressionErrorMinimum`](/documentation/Foundation/NSCompressionErrorMinimum-swift.var)

The start of the range of error codes reserved for compression errors.

[`NSCompressionFailedError`](/documentation/Foundation/NSCompressionFailedError-swift.var)

An error code value that indicates a failure to compress data using the provided algorithm.

[`NSDecompressionFailedError`](/documentation/Foundation/NSDecompressionFailedError-swift.var)

An error code value that indicates a failure to decompress data using the provided algorithm.

[`NSCompressionErrorMaximum`](/documentation/Foundation/NSCompressionErrorMaximum-c.enum.case)

The end of the range of error codes reserved for compression errors.

[`NSCompressionErrorMinimum`](/documentation/Foundation/NSCompressionErrorMinimum-c.enum.case)

The start of the range of error codes reserved for compression errors.

[`NSCompressionFailedError`](/documentation/Foundation/NSCompressionFailedError-c.enum.case)

An error code value that indicates a failure to compress data using the provided algorithm.

[`NSDecompressionFailedError`](/documentation/Foundation/NSDecompressionFailedError-c.enum.case)

An error code value that indicates a failure to decompress data using the provided algorithm.



---

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)