Class

NSData

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

Overview

NSData and its mutable subclass NSMutableData 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.

The size of the data is subject to a theoretical limit of about 8 exabytes (1 EB = 10¹⁸ bytes; in practice, the limit should not be a factor).

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

Writing Data Atomically

NSData provides methods for atomically saving their contents to a file, which guarantee that the data is either saved in its entirety, or it fails completely. An atomic write first writes the data to a temporary file and then, only if this write succeeds, moves the temporary file to its final location.

Although 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. When you work with a publicly accessible file, 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 write(to:atomically:) method (and the related methods) when working inside a publicly accessible directory. Instead, use FileHandle with an existing file descriptor to securely write the file.

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

Topics

Creating Data

init(bytes: UnsafeRawPointer?, length: Int)

Initializes a data object filled with a given number of bytes copied from a given buffer.

init(bytesNoCopy: UnsafeMutableRawPointer, length: Int)

Initializes a data object filled with a given number of bytes of data from a given buffer.

init(bytesNoCopy: UnsafeMutableRawPointer, length: Int, deallocator: ((UnsafeMutableRawPointer, Int) -> Void)? = nil)

Initializes a data object filled with a given number of bytes of data from a given buffer, with a custom deallocator block.

init(bytesNoCopy: UnsafeMutableRawPointer, length: Int, freeWhenDone: Bool)

Initializes a newly allocated data object by adding the given number of bytes from the given buffer.

init(data: Data)

Initializes a data object with the contents of another data object.

Reading Data from a File

init?(contentsOfFile: String)

Initializes a data object with the content of the file at a given path.

init(contentsOfFile: String, options: NSData.ReadingOptions = [])

Initializes a data object with the content of the file at a given path.

init?(contentsOf: URL)

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

init(contentsOf: URL, options: NSData.ReadingOptions = [])

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

struct NSData.ReadingOptions

Options for methods used to read data objects.

init?(contentsOfMappedFile: String)

Initializes a data object with the contents of the mapped file specified by a given path.

Deprecated
class func dataWithContentsOfMappedFile(String)

Creates and returns a data object from the mapped file at a given path.

Deprecated

Writing Data to a File

func write(toFile: String, atomically: Bool)

Writes the data object's bytes to the file specified by a given path.

func write(toFile: String, options: NSData.WritingOptions = [])

Writes the data object's bytes to the file specified by a given path.

func write(to: URL, atomically: Bool)

Writes the data object's bytes to the location specified by a given URL.

func write(to: URL, options: NSData.WritingOptions = [])

Writes the data object's bytes to the location specified by a given URL.

struct NSData.WritingOptions

Options for methods used to write data objects.

Encoding and Decoding Base64 Representations

init?(base64Encoded: Data, options: NSData.Base64DecodingOptions = [])

Initializes a data object with the given Base64 encoded data.

init?(base64Encoding: String)

Initializes a data object initialized with the given Base64 encoded string.

Deprecated
init?(base64Encoded: String, options: NSData.Base64DecodingOptions = [])

Initializes a data object with the given Base64 encoded string.

func base64EncodedData(options: NSData.Base64EncodingOptions = [])

Creates a Base64, UTF-8 encoded data object from the string using the given options.

func base64EncodedString(options: NSData.Base64EncodingOptions = [])

Creates a Base64 encoded string from the string using the given options.

func base64Encoding()

Initializes a Base64 encoded string from the string.

Deprecated
struct NSData.Base64EncodingOptions

Options for methods used to Base64 encode data.

struct NSData.Base64DecodingOptions

Options to modify the decoding algorithm used to decode Base64 encoded data.

Accessing Underlying Bytes

var bytes: UnsafeRawPointer

A pointer to the data object's contents.

func enumerateBytes((UnsafeRawPointer, NSRange, UnsafeMutablePointer<ObjCBool>) -> Void)

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

func getBytes(UnsafeMutableRawPointer)

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

Deprecated
func getBytes(UnsafeMutableRawPointer, length: Int)

Copies a number of bytes from the start of the data object into a given buffer.

func getBytes(UnsafeMutableRawPointer, range: NSRange)

Copies a range of bytes from the data object into a given buffer.

Finding Data

func subdata(with: NSRange)

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

func range(of: Data, options: NSData.SearchOptions = [], in: NSRange)

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

struct NSData.SearchOptions

Options for method used to search data objects.

Testing Data

func isEqual(to: Data)

Returns a Boolean value indicating whether this data object is the same as another.

var length: Int

The number of bytes contained by the data object.

func contains(UInt8)

Returns a Boolean value indicating whether the sequence contains the given element.

func contains(where: (UInt8) -> Bool)

Returns a Boolean value indicating whether the sequence contains an element that satisfies the given predicate.

Describing Data

var description: String

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

See Also

Using Reference Types

class NSMutableData

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

typealias Data.ReferenceType

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