---
title: Data
framework: foundation
role: symbol
role_heading: Structure
path: foundation/data
---

# Data

A byte buffer in memory.

## Declaration

```swift
@frozen struct Data
```

## Mentioned in

Processing URL session data task results with Combine Encoding and Decoding Custom Types

## Overview

Overview The 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 bridges to the NSData class and its mutable subclass, NSMutableData. You can use these interchangeably in code that interacts with Objective-C APIs.

## Topics

### Creating Empty Data

- [init()](foundation/data/init().md)
- [init(capacity:)](foundation/data/init(capacity:).md)
- [init(count:)](foundation/data/init(count:).md)
- [resetBytes(in:)](foundation/data/resetbytes(in:).md)

### Creating Populated Data

- [init()](foundation/data/init().md)
- [init(buffer:)](foundation/data/init(buffer:)-75sng.md)
- [init(buffer:)](foundation/data/init(buffer:)-6xgv4.md)
- [init(bytes:count:)](foundation/data/init(bytes:count:).md)
- [init(bytesNoCopy:count:deallocator:)](foundation/data/init(bytesnocopy:count:deallocator:).md)
- [init(capacity:)](foundation/data/init(capacity:).md)
- [init(count:)](foundation/data/init(count:).md)

### Creating Data from Raw Memory

- [init(bytes:count:)](foundation/data/init(bytes:count:).md)
- [init(buffer:)](foundation/data/init(buffer:)-75sng.md)
- [init(buffer:)](foundation/data/init(buffer:)-6xgv4.md)
- [init(bytesNoCopy:count:deallocator:)](foundation/data/init(bytesnocopy:count:deallocator:).md)
- [Data.Deallocator](foundation/data/deallocator.md)

### Reading and Writing Data

- [write(to:options:)](foundation/data/write(to:options:).md)
- [Data.ReadingOptions](foundation/data/readingoptions.md)
- [Data.WritingOptions](foundation/data/writingoptions.md)

### Base-64 Encoding

- [base64EncodedData(options:)](foundation/data/base64encodeddata(options:).md)
- [base64EncodedString(options:)](foundation/data/base64encodedstring(options:).md)
- [Data.Base64DecodingOptions](foundation/data/base64decodingoptions.md)
- [Data.Base64EncodingOptions](foundation/data/base64encodingoptions.md)

### Accessing Bytes

- [subscript(_:)](foundation/data/subscript(_:)-6lc96.md)
- [subscript(_:)](foundation/data/subscript(_:)-8kg64.md)

### Accessing Underlying Memory

- [withUnsafeBytes(_:)](foundation/data/withunsafebytes(_:).md)
- [withUnsafeMutableBytes(_:)](foundation/data/withunsafemutablebytes(_:)-7ac1g.md)
- [copyBytes(to:count:)](foundation/data/copybytes(to:count:).md)
- [copyBytes(to:from:)](foundation/data/copybytes(to:from:)-8qk4r.md)
- [copyBytes(to:from:)](foundation/data/copybytes(to:from:)-4o6zj.md)

### Adding Bytes

- [append(_:)](foundation/data/append(_:)-vjwy.md)
- [append(_:)](foundation/data/append(_:)-xtlw.md)
- [append(_:count:)](foundation/data/append(_:count:).md)
- [reserveCapacity(_:)](foundation/data/reservecapacity(_:).md)

### Replacing a Range of Bytes

- [replaceSubrange(_:with:)](foundation/data/replacesubrange(_:with:)-9u7ry.md)
- [replaceSubrange(_:with:)](foundation/data/replacesubrange(_:with:)-9nzh.md)
- [replaceSubrange(_:with:count:)](foundation/data/replacesubrange(_:with:count:).md)

### Finding Bytes

- [range(of:options:in:)](foundation/data/range(of:options:in:).md)
- [Data.SearchOptions](foundation/data/searchoptions.md)

### Excluding Bytes

- [advanced(by:)](foundation/data/advanced(by:).md)

### Iterating Over Bytes

- [makeIterator()](foundation/data/makeiterator().md)
- [Data.Iterator](foundation/data/iterator.md)
- [enumerateBytes(_:)](foundation/data/enumeratebytes(_:).md)

### Splitting the Buffer

- [subdata(in:)](foundation/data/subdata(in:).md)

### Comparing Data

- [==(_:_:)](foundation/data/==(_:_:).md)

### Manipulating Indexes

- [Data.Index](foundation/data/index.md)
- [startIndex](foundation/data/startindex.md)
- [endIndex](foundation/data/endindex.md)
- [index(after:)](foundation/data/index(after:).md)
- [index(before:)](foundation/data/index(before:).md)

### Manipulating Index Ranges

- [Data.Indices](foundation/data/indices.md)

### Describing Data

- [description](foundation/data/description.md)
- [debugDescription](foundation/data/debugdescription.md)

### Using Reference Types

- [NSData](foundation/nsdata.md)
- [NSMutableData](foundation/nsmutabledata.md)

### Initializers

- [init(_:)](foundation/data/init(_:)-2r3sw.md)
- [init(_:)](foundation/data/init(_:)-53ewf.md)
- [init(base64Encoded:options:)](foundation/data/init(base64encoded:options:)-1g88z.md)
- [init(base64Encoded:options:)](foundation/data/init(base64encoded:options:)-654f.md)
- [init(bytes:)](foundation/data/init(bytes:)-5krj4.md)
- [init(bytes:)](foundation/data/init(bytes:)-5s0rs.md)
- [init(bytes:)](foundation/data/init(bytes:)-9othw.md)
- [init(capacity:initializingWith:)](foundation/data/init(capacity:initializingwith:).md)
- [init(contentsOf:options:)](foundation/data/init(contentsof:options:).md)
- [init(rawCapacity:initializingWith:)](foundation/data/init(rawcapacity:initializingwith:).md)
- [init(referencing:)](foundation/data/init(referencing:).md)
- [init(repeating:count:)](foundation/data/init(repeating:count:).md)

### Instance Properties

- [bytes](foundation/data/bytes.md)
- [count](foundation/data/count.md)
- [mutableBytes](foundation/data/mutablebytes.md)
- [mutableSpan](foundation/data/mutablespan.md)
- [span](foundation/data/span.md)

### Instance Methods

- [append(contentsOf:)](foundation/data/append(contentsof:)-2ebzw.md)
- [append(contentsOf:)](foundation/data/append(contentsof:)-xeqk.md)
- [replaceSubrange(_:with:)](foundation/data/replacesubrange(_:with:)-21ouz.md)
- [withUnsafeMutableBytes(_:)](foundation/data/withunsafemutablebytes(_:)-79c12.md)

### Subscripts

- [subscript(_:)](foundation/data/subscript(_:)-59z5z.md)

### Default Implementations

- [Attachable Implementations](foundation/data/attachable-implementations.md)
- [Collection Implementations](foundation/data/collection-implementations.md)
- [CustomDebugStringConvertible Implementations](foundation/data/customdebugstringconvertible-implementations.md)
- [CustomStringConvertible Implementations](foundation/data/customstringconvertible-implementations.md)
- [Equatable Implementations](foundation/data/equatable-implementations.md)
- [Hashable Implementations](foundation/data/hashable-implementations.md)

## Relationships

### Conforms To

- [Attachable](testing/attachable.md)
- [BidirectionalCollection](swift/bidirectionalcollection.md)
- [CKRecordValueProtocol](cloudkit/ckrecordvalueprotocol.md)
- [CVAttachmentValueRepresentable](corevideo/cvattachmentvaluerepresentable.md)
- [Collection](swift/collection.md)
- [ContiguousBytes](foundation/contiguousbytes.md)
- [Copyable](swift/copyable.md)
- [CustomDebugStringConvertible](swift/customdebugstringconvertible.md)
- [CustomReflectable](swift/customreflectable.md)
- [CustomStringConvertible](swift/customstringconvertible.md)
- [DataProtocol](foundation/dataprotocol.md)
- [Decodable](swift/decodable.md)
- [Encodable](swift/encodable.md)
- [Equatable](swift/equatable.md)
- [Escapable](swift/escapable.md)
- [Hashable](swift/hashable.md)
- [MutableCollection](swift/mutablecollection.md)
- [MutableDataProtocol](foundation/mutabledataprotocol.md)
- [RandomAccessCollection](swift/randomaccesscollection.md)
- [RangeReplaceableCollection](swift/rangereplaceablecollection.md)
- [ReferenceConvertible](foundation/referenceconvertible.md)
- [Sendable](swift/sendable.md)
- [SendableMetatype](swift/sendablemetatype.md)
- [Sequence](swift/sequence.md)
- [Transferable](coretransferable/transferable.md)

## See Also

### Binary Data

- [DataProtocol](foundation/dataprotocol.md)
- [MutableDataProtocol](foundation/mutabledataprotocol.md)
- [ContiguousBytes](foundation/contiguousbytes.md)
