---
title: NSData
framework: foundation
role: symbol
role_heading: Class
path: foundation/nsdata
---

# NSData

A static byte buffer in memory.

## Declaration

```swift
class NSData
```

## Mentioned in

Implementing Handoff in Your App

## Overview

Overview In Swift, the buffer bridges to Data; use NSData when you need reference semantics or other Foundation-specific behavior. 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. important: The Swift overlay to the Foundation framework provides the Data structure, which bridges to the NSData class and its mutable subclass NSMutableData. For more information about value types, see Working with Foundation Types. 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:length:)](foundation/nsdata/init(bytes:length:).md)
- [init(bytesNoCopy:length:)](foundation/nsdata/init(bytesnocopy:length:).md)
- [init(bytesNoCopy:length:deallocator:)](foundation/nsdata/init(bytesnocopy:length:deallocator:).md)
- [init(bytesNoCopy:length:freeWhenDone:)](foundation/nsdata/init(bytesnocopy:length:freewhendone:).md)
- [init(data:)](foundation/nsdata/init(data:).md)

### Reading Data from a File

- [init(contentsOfFile:)](foundation/nsdata/init(contentsoffile:).md)
- [init(contentsOfFile:options:)](foundation/nsdata/init(contentsoffile:options:).md)
- [NSData.ReadingOptions](foundation/nsdata/readingoptions.md)
- [init(contentsOfMappedFile:)](foundation/nsdata/init(contentsofmappedfile:).md)
- [dataWithContentsOfMappedFile(_:)](foundation/nsdata/datawithcontentsofmappedfile(_:).md)

### Writing Data to a File

- [write(toFile:atomically:)](foundation/nsdata/write(tofile:atomically:).md)
- [write(toFile:options:)](foundation/nsdata/write(tofile:options:).md)
- [write(to:atomically:)](foundation/nsdata/write(to:atomically:).md)
- [write(to:options:)](foundation/nsdata/write(to:options:).md)
- [NSData.WritingOptions](foundation/nsdata/writingoptions.md)

### Encoding and Decoding Base64 Representations

- [init(base64EncodedData:options:)](foundation/nsdata/init(base64encodeddata:options:).md)
- [init(base64Encoding:)](foundation/nsdata/init(base64encoding:).md)
- [init(base64EncodedString:options:)](foundation/nsdata/init(base64encodedstring:options:).md)
- [base64EncodedData(options:)](foundation/nsdata/base64encodeddata(options:).md)
- [base64EncodedString(options:)](foundation/nsdata/base64encodedstring(options:).md)
- [base64Encoding()](foundation/nsdata/base64encoding().md)
- [NSData.Base64EncodingOptions](foundation/nsdata/base64encodingoptions.md)
- [NSData.Base64DecodingOptions](foundation/nsdata/base64decodingoptions.md)

### Accessing Underlying Bytes

- [bytes](foundation/nsdata/bytes.md)
- [enumerateBytes(_:)](foundation/nsdata/enumeratebytes(_:).md)
- [getBytes(_:)](foundation/nsdata/getbytes(_:).md)
- [getBytes(_:length:)](foundation/nsdata/getbytes(_:length:).md)
- [getBytes(_:range:)](foundation/nsdata/getbytes(_:range:).md)

### Finding Data

- [subdata(with:)](foundation/nsdata/subdata(with:).md)
- [range(of:options:in:)](foundation/nsdata/range(of:options:in:).md)
- [NSData.SearchOptions](foundation/nsdata/searchoptions.md)

### Testing Data

- [isEqual(to:)](foundation/nsdata/isequal(to:).md)
- [length](foundation/nsdata/length.md)

### Describing Data

- [description](foundation/nsdata/description.md)

### Compressing and Decompressing Data

- [compressed(using:)](foundation/nsdata/compressed(using:).md)
- [decompressed(using:)](foundation/nsdata/decompressed(using:).md)
- [NSData.CompressionAlgorithm](foundation/nsdata/compressionalgorithm.md)
- [NSCompressionErrorMaximum](foundation/nscompressionerrormaximum-swift.var.md)
- [NSCompressionErrorMinimum](foundation/nscompressionerrorminimum-swift.var.md)
- [NSCompressionFailedError](foundation/nscompressionfailederror-swift.var.md)
- [NSDecompressionFailedError](foundation/nsdecompressionfailederror-swift.var.md)

### Initializers

- [init(base64Encoded:options:)](foundation/nsdata/init(base64encoded:options:)-3ksry.md)
- [init(base64Encoded:options:)](foundation/nsdata/init(base64encoded:options:)-4t5yq.md)
- [init(coder:)](foundation/nsdata/init(coder:).md)
- [init(contentsOf:)](foundation/nsdata/init(contentsof:).md)
- [init(contentsOf:options:)](foundation/nsdata/init(contentsof:options:).md)

### Default Implementations

- [NSData Implementations](foundation/nsdata/nsdata-implementations.md)

## Relationships

### Inherits From

- [NSObject](objectivec/nsobject-swift.class.md)

### Inherited By

- [NSMutableData](foundation/nsmutabledata.md)

### Conforms To

- [BidirectionalCollection](swift/bidirectionalcollection.md)
- [CKRecordValue](cloudkit/ckrecordvalue-c.protocol.md)
- [CKRecordValueProtocol](cloudkit/ckrecordvalueprotocol.md)
- [CVarArg](swift/cvararg.md)
- [Collection](swift/collection.md)
- [Copyable](swift/copyable.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [DataProtocol](foundation/dataprotocol.md)
- [Equatable](swift/equatable.md)
- [Escapable](swift/escapable.md)
- [Hashable](swift/hashable.md)
- [NSCoding](foundation/nscoding.md)
- [NSCopying](foundation/nscopying.md)
- [NSMutableCopying](foundation/nsmutablecopying.md)
- [NSObjectProtocol](objectivec/nsobjectprotocol.md)
- [NSSecureCoding](foundation/nssecurecoding.md)
- [RandomAccessCollection](swift/randomaccesscollection.md)
- [Sequence](swift/sequence.md)
