<!--
{
  "availability" : [
    "iOS: 8.0.0 -",
    "iPadOS: 8.0.0 -",
    "macCatalyst: 8.0.0 -",
    "macOS: 10.10.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/Data",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:10Foundation4DataV"
  },
  "title" : "Data"
}
-->

# Data

A byte buffer in memory.

```
@frozen struct Data
```

## Overview

The [`Data`](/documentation/Foundation/Data) value type allows simple byte buffers to take on the behavior of Foundation objects. You can create empty or pre-populated buffers from a variety of sources and later add or remove bytes. You can filter and sort the content, or compare against other buffers. You can manipulate subranges of bytes and iterate over some or all of them.

[`Data`](/documentation/Foundation/Data) bridges to the [`NSData`](/documentation/Foundation/NSData) class and its mutable subclass, [`NSMutableData`](/documentation/Foundation/NSMutableData). You can use these interchangeably in code that interacts with Objective-C APIs.

## Topics

### Creating Empty Data

[`init()`](/documentation/Foundation/Data/init())

Creates an empty data buffer.

[`init(capacity:)`](/documentation/Foundation/Data/init(capacity:))

Creates an empty data buffer of a specified size.

[`init(count:)`](/documentation/Foundation/Data/init(count:))

Creates a new data buffer with the specified count of zeroed bytes.

[`resetBytes(in:)`](/documentation/Foundation/Data/resetBytes(in:))

Sets a region of the data buffer to `0`.

### Creating Populated Data

[`init()`](/documentation/Foundation/Data/init())

Creates an empty data buffer.

[`init(buffer:)`](/documentation/Foundation/Data/init(buffer:)-75sng)

Creates a data buffer with copied memory content using a buffer pointer.

[`init(buffer:)`](/documentation/Foundation/Data/init(buffer:)-6xgv4)

Creates a data buffer with copied memory content using a mutable buffer pointer.

[`init(bytes:count:)`](/documentation/Foundation/Data/init(bytes:count:))

Creates data with copied memory content.

[`init(bytesNoCopy:count:deallocator:)`](/documentation/Foundation/Data/init(bytesNoCopy:count:deallocator:))

Creates a data buffer with memory content without copying the bytes.

[`init(capacity:)`](/documentation/Foundation/Data/init(capacity:))

Creates an empty data buffer of a specified size.

[`init(count:)`](/documentation/Foundation/Data/init(count:))

Creates a new data buffer with the specified count of zeroed bytes.

### Creating Data from Raw Memory

[`init(bytes:count:)`](/documentation/Foundation/Data/init(bytes:count:))

Creates data with copied memory content.

[`init(buffer:)`](/documentation/Foundation/Data/init(buffer:)-75sng)

Creates a data buffer with copied memory content using a buffer pointer.

[`init(buffer:)`](/documentation/Foundation/Data/init(buffer:)-6xgv4)

Creates a data buffer with copied memory content using a mutable buffer pointer.

[`init(bytesNoCopy:count:deallocator:)`](/documentation/Foundation/Data/init(bytesNoCopy:count:deallocator:))

Creates a data buffer with memory content without copying the bytes.

[`Data.Deallocator`](/documentation/Foundation/Data/Deallocator)

A deallocator you use to customize how the backing store is deallocated for data created with the no-copy initializer.

### Reading and Writing Data

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

Writes the contents of the data buffer to a location.

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

Options to control the reading of data from a URL.

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

Options to control the writing of data to a URL.

### Base-64 Encoding

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

Returns Base-64 encoded data.

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

Returns a Base-64 encoded string.

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

Options to use when decoding data.

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

Options to use when encoding data.

### Counting Bytes

### Accessing Bytes

[`subscript(_:)`](/documentation/Foundation/Data/subscript(_:)-6lc96)

Accesses the bytes at the specified range of indexes.

[`subscript(_:)`](/documentation/Foundation/Data/subscript(_:)-8kg64)

Accesses the byte at the specified index.

### Accessing Underlying Memory

[`withUnsafeBytes(_:)`](/documentation/Foundation/Data/withUnsafeBytes(_:))

Accesses the raw bytes in the data’s buffer.

[`withUnsafeMutableBytes(_:)`](/documentation/Foundation/Data/withUnsafeMutableBytes(_:)-7ac1g)

Mutates the raw bytes in the data’s buffer.

[`copyBytes(to:count:)`](/documentation/Foundation/Data/copyBytes(to:count:))

Copies the contents of the data to memory.

[`copyBytes(to:from:)`](/documentation/Foundation/Data/copyBytes(to:from:)-8qk4r)

Copies a subset of the contents of the data to memory.

[`copyBytes(to:from:)`](/documentation/Foundation/Data/copyBytes(to:from:)-4o6zj)

Copies the bytes in a range from the data into a buffer.

### Adding Bytes

[`append(_:)`](/documentation/Foundation/Data/append(_:)-vjwy)

Appends the specified data to the end of this data.

[`append(_:)`](/documentation/Foundation/Data/append(_:)-xtlw)

Append a buffer of bytes to the data.

[`append(_:count:)`](/documentation/Foundation/Data/append(_:count:))

Appends the specified bytes from memory to the end of the data.

[`reserveCapacity(_:)`](/documentation/Foundation/Data/reserveCapacity(_:))

Prepares the collection to store the specified number of elements, when doing so is appropriate for the underlying type.

### Removing Bytes

### Replacing a Range of Bytes

[`replaceSubrange(_:with:)`](/documentation/Foundation/Data/replaceSubrange(_:with:)-9u7ry)

Replaces a region of bytes in the data with new bytes from a collection.

[`replaceSubrange(_:with:)`](/documentation/Foundation/Data/replaceSubrange(_:with:)-9nzh)

Replaces a region of bytes in the data with new bytes from a buffer.

[`replaceSubrange(_:with:count:)`](/documentation/Foundation/Data/replaceSubrange(_:with:count:))

Replaces a region of bytes in the data with bytes from memory.

### Finding Bytes

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

Finds the range of the specified data as a subsequence of this data, if it exists.

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

Options that control a data search operation.

### Selecting Bytes

### Excluding Bytes

[`advanced(by:)`](/documentation/Foundation/Data/advanced(by:))

Returns a new data buffer created by removing the given number of bytes from the front of the original buffer.

### Transforming Data

### Iterating Over Bytes

[`makeIterator()`](/documentation/Foundation/Data/makeIterator())

Returns an iterator over the contents of the data.

[`Data.Iterator`](/documentation/Foundation/Data/Iterator)

An iterator that operates over the contents of data.

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

Enumerates the contents of the data’s buffer.

### Sorting Bytes

### Splitting the Buffer

[`subdata(in:)`](/documentation/Foundation/Data/subdata(in:))

Returns a new copy of the data in a specified range.

### Comparing Data

[`==(_:_:)`](/documentation/Foundation/Data/==(_:_:))

Returns `true` if the two `Data` arguments are equal.

### Manipulating Indexes

[`Data.Index`](/documentation/Foundation/Data/Index)

A type used to indicate a position in a data’s buffer.

[`startIndex`](/documentation/Foundation/Data/startIndex)

The beginning index into the data.

[`endIndex`](/documentation/Foundation/Data/endIndex)

The end index into the data.

[`index(after:)`](/documentation/Foundation/Data/index(after:))

Returns the index that immediately follows the specified index.

[`index(before:)`](/documentation/Foundation/Data/index(before:))

Returns the index that immediately precedes the specified index.

### Manipulating Index Ranges

[`Data.Indices`](/documentation/Foundation/Data/Indices)

A type used to indicate a range of positions in a data’s buffer.

### Describing Data

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

A human-readable description for the data.

[`debugDescription`](/documentation/Foundation/Data/debugDescription)

A human-readable debug description for the data.

### Using Reference Types

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

A static byte buffer in memory.

[`NSMutableData`](/documentation/Foundation/NSMutableData)

An object representing a dynamic byte buffer in memory.



---

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)