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:

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.


Creating and Initializing an NSMutableData Object

init?(capacity: Int)

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

init?(length: Int)

Initializes and returns an NSMutableData object containing a given number of zeroed bytes.

Adjusting Capacity

var length: Int

The number of bytes contained in the mutable data object.

func increaseLength(by: Int)

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

Accessing Data

var mutableBytes: UnsafeMutableRawPointer

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

Adding Data

func append(UnsafeRawPointer, length: Int)

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

func append(Data)

Appends the content of another NSData object to the receiver.

Modifying Data

func replaceBytes(in: NSRange, withBytes: UnsafeRawPointer)

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

func replaceBytes(in: NSRange, withBytes: UnsafeRawPointer?, length: Int)

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

func resetBytes(in: NSRange)

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

func setData(Data)

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


Inherits From

Conforms To