Class

NSMutableData

A dynamic byte buffer that bridges to Data; use NSMutableData when you need reference semantics or other Foundation-specific behavior.

Overview

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.

Topics

Creating Mutable Data

init?(capacity: Int)

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

init?(length: Int)

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

Accessing Raw Bytes

var mutableBytes: UnsafeMutableRawPointer

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

Counting Bytes

var length: Int

The number of bytes contained in the mutable data object.

Adding Bytes

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 data object to the receiver.

func increaseLength(by: Int)

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

Modifying Bytes

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.

Relationships

Inherits From

Conforms To

See Also

Using Reference Types

class NSData

A static byte buffer that bridges to Data; use NSData when you need reference semantics or other Foundation-specific behavior.

typealias Data.ReferenceType

An alias for this value type's equivalent reference type.