NSMutable​Data 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 NSMutable​Data 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 NSMutable​Data 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.

NSMutable​Data 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 NSMutable​Data object capable of holding the specified number of bytes.

init?(length:​ Int)

Initializes and returns an NSMutable​Data object containing a given number of zeroed bytes.

Adjusting Capacity

var length:​ Int

The number of bytes contained in the mutable data object.

func increase​Length(by:​ Int)

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

Accessing Data

var mutable​Bytes:​ Unsafe​Mutable​Raw​Pointer

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

Adding Data

func append(Unsafe​Raw​Pointer, 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 replace​Bytes(in:​ NSRange, with​Bytes:​ Unsafe​Raw​Pointer)

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

func replace​Bytes(in:​ NSRange, with​Bytes:​ Unsafe​Raw​Pointer?, length:​ Int)

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

func reset​Bytes(in:​ NSRange)

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

func set​Data(Data)

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


Inherits From