<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSData",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSData"
  },
  "title" : "NSData"
}
-->

# NSData

A static byte buffer in memory.

```
class NSData
```

## Overview

In Swift, the buffer bridges to [`Data`](/documentation/Foundation/Data); use [`NSData`](/documentation/Foundation/NSData) when you need reference semantics or other Foundation-specific behavior.

[NSData](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/PropertyLists/OldStylePlists/OldStylePLists.html#//apple_ref/doc/uid/20001012-47169) and its mutable subclass [`NSMutableData`](/documentation/Foundation/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](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/PropertyLists/OldStylePlists/OldStylePLists.html#//apple_ref/doc/uid/20001012-47169) is *toll-free bridged* with its Core Foundation counterpart, <doc://com.apple.documentation/documentation/CoreFoundation/CFData>. See [Toll-Free Bridging](https://developer.apple.com/library/archive/documentation/General/Conceptual/CocoaEncyclopedia/Toll-FreeBridgin/Toll-FreeBridgin.html#//apple_ref/doc/uid/TP40010810-CH2) for more information on toll-free bridging.

> Important:
> The Swift overlay to the Foundation framework provides the ``doc://com.apple.foundation/documentation/Foundation/Data`` structure, which bridges to the ``doc://com.apple.foundation/documentation/Foundation/NSData`` class and its mutable subclass ``doc://com.apple.foundation/documentation/Foundation/NSMutableData``. For more information about value types, see <doc://com.apple.documentation/documentation/Swift/working-with-foundation-types>.

### Writing Data Atomically

[NSData](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/PropertyLists/OldStylePlists/OldStylePLists.html#//apple_ref/doc/uid/20001012-47169) 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:)`](/documentation/Foundation/NSData/write(to:atomically:)) method (and the related methods) when working inside a publicly accessible directory. Instead, use [`FileHandle`](/documentation/Foundation/FileHandle) with an existing file descriptor to securely write the file.

For more information, see [Securing File Operations](https://developer.apple.com/library/archive/documentation/Security/Conceptual/SecureCodingGuide/Articles/RaceConditions.html#//apple_ref/doc/uid/TP40002585-SW9) in [Secure Coding Guide](https://developer.apple.com/library/archive/documentation/Security/Conceptual/SecureCodingGuide/Introduction.html#//apple_ref/doc/uid/TP40002415).

## Topics

### Creating Data

[`data`](/documentation/Foundation/NSData/data)

Creates an empty data object.

[`dataWithBytes:length:`](/documentation/Foundation/NSData/dataWithBytes:length:)

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

[`dataWithBytesNoCopy:length:`](/documentation/Foundation/NSData/dataWithBytesNoCopy:length:)

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

[`dataWithBytesNoCopy:length:freeWhenDone:`](/documentation/Foundation/NSData/dataWithBytesNoCopy:length:freeWhenDone:)

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

[`dataWithData:`](/documentation/Foundation/NSData/dataWithData:)

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

[`init(bytes:length:)`](/documentation/Foundation/NSData/init(bytes:length:))

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

[`init(bytesNoCopy:length:)`](/documentation/Foundation/NSData/init(bytesNoCopy:length:))

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

[`init(bytesNoCopy:length:deallocator:)`](/documentation/Foundation/NSData/init(bytesNoCopy:length:deallocator:))

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

[`init(bytesNoCopy:length:freeWhenDone:)`](/documentation/Foundation/NSData/init(bytesNoCopy:length:freeWhenDone:))

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

[`init(data:)`](/documentation/Foundation/NSData/init(data:))

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

### Reading Data from a File

[`dataWithContentsOfFile:`](/documentation/Foundation/NSData/dataWithContentsOfFile:)

Creates a data object by reading every byte from the file at a given path.

[`dataWithContentsOfFile:options:error:`](/documentation/Foundation/NSData/dataWithContentsOfFile:options:error:)

Creates a data object by reading every byte from the file at a given path.

[`init(contentsOfFile:)`](/documentation/Foundation/NSData/init(contentsOfFile:))

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

[`init(contentsOfFile:options:)`](/documentation/Foundation/NSData/init(contentsOfFile:options:))

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

[`NSData.ReadingOptions`](/documentation/Foundation/NSData/ReadingOptions)

Options for methods used to read data objects.

[`init(contentsOfMappedFile:)`](/documentation/Foundation/NSData/init(contentsOfMappedFile:))

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

[`dataWithContentsOfMappedFile(_:)`](/documentation/Foundation/NSData/dataWithContentsOfMappedFile(_:))

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

### Writing Data to a File

[`write(toFile:atomically:)`](/documentation/Foundation/NSData/write(toFile:atomically:))

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

[`write(toFile:options:)`](/documentation/Foundation/NSData/write(toFile:options:))

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

[`write(to:atomically:)`](/documentation/Foundation/NSData/write(to:atomically:))

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

[`write(to:options:)`](/documentation/Foundation/NSData/write(to:options:))

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

[`NSData.WritingOptions`](/documentation/Foundation/NSData/WritingOptions)

Options for methods used to write data objects.

### Encoding and Decoding Base64 Representations

[`init(base64EncodedData:options:)`](/documentation/Foundation/NSData/init(base64EncodedData:options:))

Initializes a data object with the given Base64 encoded data.

[`init(base64Encoding:)`](/documentation/Foundation/NSData/init(base64Encoding:))

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

[`init(base64EncodedString:options:)`](/documentation/Foundation/NSData/init(base64EncodedString:options:))

Initializes a data object with the given Base64 encoded string.

[`base64EncodedData(options:)`](/documentation/Foundation/NSData/base64EncodedData(options:))

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

[`base64EncodedString(options:)`](/documentation/Foundation/NSData/base64EncodedString(options:))

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

[`base64Encoding()`](/documentation/Foundation/NSData/base64Encoding())

Initializes a Base64 encoded string from the string.

[`NSData.Base64EncodingOptions`](/documentation/Foundation/NSData/Base64EncodingOptions)

Options for methods used to Base64 encode data.

[`NSData.Base64DecodingOptions`](/documentation/Foundation/NSData/Base64DecodingOptions)

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

### Accessing Underlying Bytes

[`bytes`](/documentation/Foundation/NSData/bytes)

A pointer to the data object’s contents.

[`enumerateBytes(_:)`](/documentation/Foundation/NSData/enumerateBytes(_:))

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

[`getBytes(_:)`](/documentation/Foundation/NSData/getBytes(_:))

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

[`getBytes(_:length:)`](/documentation/Foundation/NSData/getBytes(_:length:))

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

[`getBytes(_:range:)`](/documentation/Foundation/NSData/getBytes(_:range:))

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

### Finding Data

[`subdata(with:)`](/documentation/Foundation/NSData/subdata(with:))

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

[`range(of:options:in:)`](/documentation/Foundation/NSData/range(of:options:in:))

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

[`NSData.SearchOptions`](/documentation/Foundation/NSData/SearchOptions)

Options for method used to search data objects.

### Testing Data

[`isEqual(to:)`](/documentation/Foundation/NSData/isEqual(to:))

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

[`length`](/documentation/Foundation/NSData/length)

The number of bytes contained by the data object.

### Describing Data

[`description`](/documentation/Foundation/NSData/description)

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

### Compressing and Decompressing Data

[`compressed(using:)`](/documentation/Foundation/NSData/compressed(using:))

Returns a new data object by compressing the data object’s bytes.

[`decompressed(using:)`](/documentation/Foundation/NSData/decompressed(using:))

Returns a new data object by decompressing data object’s bytes.

[`NSData.CompressionAlgorithm`](/documentation/Foundation/NSData/CompressionAlgorithm)

An algorithm that indicates how to compress or decompress data.

[`NSCompressionErrorMaximum`](/documentation/Foundation/NSCompressionErrorMaximum-swift.var)

The end of the range of error codes reserved for compression errors.

[`NSCompressionErrorMinimum`](/documentation/Foundation/NSCompressionErrorMinimum-swift.var)

The start of the range of error codes reserved for compression errors.

[`NSCompressionFailedError`](/documentation/Foundation/NSCompressionFailedError-swift.var)

An error code value that indicates a failure to compress data using the provided algorithm.

[`NSDecompressionFailedError`](/documentation/Foundation/NSDecompressionFailedError-swift.var)

An error code value that indicates a failure to decompress data using the provided algorithm.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)