Class

NSData

NSData and its mutable subclass NSMutableData provide data objects, 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.

Overview

NSData creates static data objects, and NSMutableData creates dynamic data objects. NSData and NSMutableData 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.

The size of the data is subject to a theoretical limit of about 8 ExaBytes (in practice, the limit should not be a factor).

NSData is “toll-free bridged” with its Core Foundation counterpart, CFDataRef. See Toll-Free Bridging for more information on toll-free bridging.

Saving Data

The NSData class and its subclasses provide methods to quickly and easily save their contents to disk. To minimize the risk of data loss, these methods provide the option of saving the data atomically. Atomic writes guarantee that the data is either saved in its entirety, or it fails completely. The atomic write begins by writing the data to a temporary file. If this write succeeds, then the method moves the temporary file to its final location.

While atomic write operations minimize the risk of data loss due to corrupt or partially-written files, they may not be appropriate when writing to a temporary directory, the user’s home directory or other publicly accessible directories. Any time you work with a publicly accessible file, you should treat that file as an untrusted and potentially dangerous resource. An attacker may compromise or corrupt these files. The attacker can also replace the files with hard or symbolic links, causing your write operations to overwrite or corrupt other system resources.

Avoid using the writeToURL:atomically: method (and the related methods) when working inside a publicly accessible directory. Instead initialize an NSFileHandle object with an existing file descriptor and use the NSFileHandle methods to securely write the file.

For more information, see Securing File Operations in Secure Coding Guide.

Symbols

Creating Data Objects

+ data

Creates and returns an empty data object.

+ dataWithBytes:length:

Creates and returns a data object containing a given number of bytes copied from a given buffer.

+ dataWithBytesNoCopy:length:

Creates and returns a data object that holds length bytes from the buffer bytes.

+ dataWithBytesNoCopy:length:freeWhenDone:

Creates and returns a data object that holds a given number of bytes from a given buffer.

+ dataWithContentsOfFile:

Creates and returns a data object by reading every byte from the file specified by a given path.

+ dataWithContentsOfFile:options:error:

Creates and returns a data object by reading every byte from the file specified by a given path.

+ dataWithContentsOfMappedFile:

Creates and returns a data object from the mapped file specified by path.

Deprecated
+ dataWithContentsOfURL:

Returns a data object containing the data from the location specified by a given URL.

+ dataWithContentsOfURL:options:error:

Creates and returns a data object containing the data from the location specified by aURL.

+ dataWithData:

Creates and returns a data object containing the contents of another data object.

- initWithBase64EncodedData:options:

Returns a data object initialized with the given Base-64 encoded data.

- initWithBase64EncodedString:options:

Returns a data object initialized with the given Base-64 encoded string.

- initWithBase64Encoding:

Returns a data object initialized with the given Base-64 encoded string.

Deprecated
- initWithBytes:length:

Returns a data object initialized by adding to it a given number of bytes of data copied from a given buffer.

- initWithBytesNoCopy:length:

Returns a data object initialized by adding to it a given number of bytes of data from a given buffer.

- initWithBytesNoCopy:length:deallocator:

Returns a data object initialized by adding to it a given number of bytes of data from a given buffer, with a custom deallocator block.

- initWithBytesNoCopy:length:freeWhenDone:

Initializes a newly allocated data object by adding to it length bytes of data from the buffer bytes.

- initWithContentsOfFile:

Returns a data object initialized by reading into it the data from the file specified by a given path.

- initWithContentsOfFile:options:error:

Returns a data object initialized by reading into it the data from the file specified by a given path.

- initWithContentsOfMappedFile:

Returns a data object initialized by reading into it the mapped file specified by a given path.

Deprecated
- initWithContentsOfURL:

Initializes a newly allocated data object initialized with the data from the location specified by aURL.

- initWithContentsOfURL:options:error:

Returns a data object initialized with the data from the location specified by a given URL.

- initWithData:

Returns a data object initialized with the contents of another data object.

Accessing Data

bytes

A pointer to the receiver’s contents.

description

A string that contains a hexadecimal representation of the object’s contents in a property list format.

- enumerateByteRangesUsingBlock:

Enumerate through each range of bytes in the data object using a block.

- getBytes:

Copies a data object’s contents into a given buffer.

Deprecated
- getBytes:length:

Copies a number of bytes from the start of the receiver's data into a given buffer.

- getBytes:range:

Copies a range of bytes from the receiver’s data into a given buffer.

- subdataWithRange:

Returns a data object containing the receiver’s bytes that fall within the limits specified by a given range.

- rangeOfData:options:range:

Finds and returns the range of the first occurrence of the given data, within the given range, subject to given options.

Base-64 Encoding

- base64EncodedDataWithOptions:

Create a Base-64, UTF-8 encoded NSData from the receiver's contents using the given options.

- base64EncodedStringWithOptions:

Create a Base-64 encoded NSString from the receiver's contents using the given options.

- base64Encoding

Create a Base-64 encoded NSString from the receiver's contents.

Deprecated

Testing Data

- isEqualToData:

Compares the receiving data object to otherData.

length

The number of bytes contained by the data object.

Storing Data

- writeToFile:atomically:

Writes the bytes in the receiver to the file specified by a given path.

- writeToFile:options:error:

Writes the bytes in the receiver to the file specified by a given path.

- writeToURL:atomically:

Writes the bytes in the receiver to the location specified by aURL.

- writeToURL:options:error:

Writes the bytes in the receiver to the location specified by a given URL.

Constants

NSDataReadingOptions

Options for methods used to read NSData objects.

NSDataBase64EncodingOptions

Options for methods used to Base-64 encode data.

NSDataBase64DecodingOptions

Options to modify the decoding algorithm used to decode Base-64 encoded NSData objects.

Legacy Reading Options

Deprecated names for reading options. Do not use these names, use the new replacements instead.

NSDataWritingOptions

Options for methods used to write NSData objects.

Legacy Writing Options

Deprecated names for writing options. Do not use these names, use the new replacements instead.

NSDataSearchOptions

Options for method used to search NSData objects. These options are used with the rangeOfData:options:range: method.

Relationships

Inherits From