---
title: NSMutableData
framework: foundation
role: symbol
role_heading: Class
path: foundation/nsmutabledata
---

# NSMutableData

An object representing a dynamic byte buffer in memory.

## Declaration

```swift
class NSMutableData
```

## Overview

Overview In Swift, this object bridges to Data; use 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 methods change when used on a mutable data object: init(bytesNoCopy:length:freeWhenDone:) init(bytesNoCopy:length:deallocator:) init(bytesNoCopy:length:) dataWithBytesNoCopy:length:freeWhenDone: 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, CFData. See Toll-Free Bridging for more information on toll-free bridging. important: The Swift overlay to the Foundation framework provides the Data structure, which bridges to the NSMutableData class and its immutable superclass NSData. For more information about value types, see Working with Foundation Types.

## Topics

### Creating Mutable Data

- [init(capacity:)](foundation/nsmutabledata/init(capacity:).md)
- [init(length:)](foundation/nsmutabledata/init(length:).md)

### Accessing Raw Bytes

- [mutableBytes](foundation/nsmutabledata/mutablebytes.md)

### Counting Bytes

- [length](foundation/nsmutabledata/length.md)

### Adding Bytes

- [append(_:length:)](foundation/nsmutabledata/append(_:length:).md)
- [append(_:)](foundation/nsmutabledata/append(_:).md)
- [increaseLength(by:)](foundation/nsmutabledata/increaselength(by:).md)

### Modifying Bytes

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

### Compressing and Decompressing Data

- [compress(using:)](foundation/nsmutabledata/compress(using:).md)
- [decompress(using:)](foundation/nsmutabledata/decompress(using:).md)
- [NSData.CompressionAlgorithm](foundation/nsdata/compressionalgorithm.md)
- [NSCompressionErrorMaximum](foundation/nscompressionerrormaximum-swift.var.md)
- [NSCompressionErrorMinimum](foundation/nscompressionerrorminimum-swift.var.md)
- [NSCompressionFailedError](foundation/nscompressionfailederror-swift.var.md)
- [NSDecompressionFailedError](foundation/nsdecompressionfailederror-swift.var.md)

## Relationships

### Inherits From

- [NSData](foundation/nsdata.md)

### Inherited By

- [NSPurgeableData](foundation/nspurgeabledata.md)

### Conforms To

- [BidirectionalCollection](swift/bidirectionalcollection.md)
- [CVarArg](swift/cvararg.md)
- [Collection](swift/collection.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [DataProtocol](foundation/dataprotocol.md)
- [Equatable](swift/equatable.md)
- [Hashable](swift/hashable.md)
- [NSCoding](foundation/nscoding.md)
- [NSCopying](foundation/nscopying.md)
- [NSMutableCopying](foundation/nsmutablecopying.md)
- [NSObjectProtocol](objectivec/nsobjectprotocol.md)
- [NSSecureCoding](foundation/nssecurecoding.md)
- [RandomAccessCollection](swift/randomaccesscollection.md)
- [Sendable](swift/sendable.md)
- [SendableMetatype](swift/sendablemetatype.md)
- [Sequence](swift/sequence.md)
