<!--
{
  "availability" : [
    "macOS: 10.15.1 -"
  ],
  "documentType" : "symbol",
  "framework" : "AppKit",
  "identifier" : "/documentation/AppKit/NSDiffableDataSourceSnapshot-swift.struct",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "AppKit"
    ],
    "preciseIdentifier" : "s:6AppKit28NSDiffableDataSourceSnapshotV"
  },
  "title" : "NSDiffableDataSourceSnapshot"
}
-->

# NSDiffableDataSourceSnapshot

A representation of the state of the data in a view at a specific point in time.

```
struct NSDiffableDataSourceSnapshot<SectionIdentifierType, ItemIdentifierType> where SectionIdentifierType : Hashable, ItemIdentifierType : Hashable
```

## Overview

Diffable data sources use *snapshots* to provide data for collection views and table views. You use a snapshot to set up the initial state of the data that a view displays, and you use snapshots to reflect changes to the data that the view displays.

The data in a snapshot is made up of the sections and items you want to display, in the order you that you determine. You configure what to display by adding, deleting, or moving the sections and items.

> Important:
> Each of your sections and items must have unique identifiers that conform to the <doc://com.apple.documentation/documentation/Swift/Hashable> protocol. Use `struct` or `enum` Swift value types for your identifiers, including built-in types such as `Int`, `String`, or `UUID`. If you use a Swift `class` for your identifiers, your `class` must be a subclass of `NSObject`.

To display data in a view using a snapshot:

1. Create a snapshot and populate it with the state of the data you want to display.
2. Apply the snapshot to reflect the changes in the UI.

You can create and configure a snapshot in one of these ways:

- Create an empty snapshot, then append sections and items to it.
- Get the current snapshot by calling the diffable data source’s [`snapshot()`](/documentation/AppKit/NSCollectionViewDiffableDataSource-axww/snapshot()) method, then modify that snapshot to reflect the new state of the data that you want to display.

For example, the following code creates an empty snapshot and populates it with a single section with three items. Then, the code applies the snapshot, animating the UI updates between the previous state and the new state.

```swift
// Create a snapshot.
var snapshot = NSDiffableDataSourceSnapshot<Int, UUID>()        

// Populate the snapshot.
snapshot.appendSections([0])
snapshot.appendItems([UUID(), UUID(), UUID()])

// Apply the snapshot.
dataSource.apply(snapshot, animatingDifferences: true)
```

For more information, see the diffable data source types:

- <doc://com.apple.documentation/documentation/UIKit/UICollectionViewDiffableDataSource-9tqpa>
- <doc://com.apple.documentation/documentation/UIKit/UITableViewDiffableDataSource-2euir>
- [`NSCollectionViewDiffableDataSourceReference`](/documentation/AppKit/NSCollectionViewDiffableDataSourceReference)

### Bridging

You can bridge from an [`NSDiffableDataSourceSnapshotReference`](/documentation/AppKit/NSDiffableDataSourceSnapshotReference) object to this type:

```swift
let snapshot = snapshotReference as NSDiffableDataSourceSnapshot<Int, UUID>
```

## Topics

### Creating a Snapshot

[`init()`](/documentation/AppKit/NSDiffableDataSourceSnapshot-swift.struct/init())

Creates an empty snapshot.

[`appendSections(_:)`](/documentation/AppKit/NSDiffableDataSourceSnapshot-swift.struct/appendSections(_:))

Adds the sections with the specified identifiers to the snapshot.

[`appendItems(_:toSection:)`](/documentation/AppKit/NSDiffableDataSourceSnapshot-swift.struct/appendItems(_:toSection:))

Adds the items with the specified identifiers to the specified section of the snapshot.

### Getting Item and Section Metrics

[`numberOfItems`](/documentation/AppKit/NSDiffableDataSourceSnapshot-swift.struct/numberOfItems)

The number of items in the snapshot.

[`numberOfSections`](/documentation/AppKit/NSDiffableDataSourceSnapshot-swift.struct/numberOfSections)

The number of sections in the snapshot.

[`numberOfItems(inSection:)`](/documentation/AppKit/NSDiffableDataSourceSnapshot-swift.struct/numberOfItems(inSection:))

Returns the number of items in the specified section of the snapshot.

### Identifying Items and Sections

[`itemIdentifiers`](/documentation/AppKit/NSDiffableDataSourceSnapshot-swift.struct/itemIdentifiers)

The identifiers of all of the items in the snapshot.

[`sectionIdentifiers`](/documentation/AppKit/NSDiffableDataSourceSnapshot-swift.struct/sectionIdentifiers)

The identifiers of all of the sections in the snapshot.

[`indexOfItem(_:)`](/documentation/AppKit/NSDiffableDataSourceSnapshot-swift.struct/indexOfItem(_:))

Returns the index of the item in the snapshot with the specified identifier.

[`indexOfSection(_:)`](/documentation/AppKit/NSDiffableDataSourceSnapshot-swift.struct/indexOfSection(_:))

Returns the index of the section of the snapshot with the specified identifier.

[`itemIdentifiers(inSection:)`](/documentation/AppKit/NSDiffableDataSourceSnapshot-swift.struct/itemIdentifiers(inSection:))

Returns the identifiers of all of the items in the specified section of the snapshot.

[`sectionIdentifier(containingItem:)`](/documentation/AppKit/NSDiffableDataSourceSnapshot-swift.struct/sectionIdentifier(containingItem:))

Returns the identifier of the section containing the specified item in the snapshot.

### Inserting Items and Sections

[`insertItems(_:afterItem:)`](/documentation/AppKit/NSDiffableDataSourceSnapshot-swift.struct/insertItems(_:afterItem:))

Inserts the provided items immediately after the item with the specified identifier in the snapshot.

[`insertItems(_:beforeItem:)`](/documentation/AppKit/NSDiffableDataSourceSnapshot-swift.struct/insertItems(_:beforeItem:))

Inserts the provided items immediately before the item with the specified identifier in the snapshot.

[`insertSections(_:afterSection:)`](/documentation/AppKit/NSDiffableDataSourceSnapshot-swift.struct/insertSections(_:afterSection:))

Inserts the provided sections immediately after the section with the specified identifier in the snapshot.

[`insertSections(_:beforeSection:)`](/documentation/AppKit/NSDiffableDataSourceSnapshot-swift.struct/insertSections(_:beforeSection:))

Inserts the provided sections immediately before the section with the specified identifier in the snapshot.

### Removing Items and Sections

[`deleteAllItems()`](/documentation/AppKit/NSDiffableDataSourceSnapshot-swift.struct/deleteAllItems())

Deletes all of the items from the snapshot.

[`deleteItems(_:)`](/documentation/AppKit/NSDiffableDataSourceSnapshot-swift.struct/deleteItems(_:))

Deletes the items with the specified identifiers from the snapshot.

[`deleteSections(_:)`](/documentation/AppKit/NSDiffableDataSourceSnapshot-swift.struct/deleteSections(_:))

Deletes the sections with the specified identifiers from the snapshot.

### Reordering Items and Sections

[`moveItem(_:afterItem:)`](/documentation/AppKit/NSDiffableDataSourceSnapshot-swift.struct/moveItem(_:afterItem:))

Moves the item from its current position in the snapshot to the position immediately after the specified item.

[`moveItem(_:beforeItem:)`](/documentation/AppKit/NSDiffableDataSourceSnapshot-swift.struct/moveItem(_:beforeItem:))

Moves the item from its current position in the snapshot to the position immediately before the specified item.

[`moveSection(_:afterSection:)`](/documentation/AppKit/NSDiffableDataSourceSnapshot-swift.struct/moveSection(_:afterSection:))

Moves the section from its current position in the snapshot to the position immediately after the specified section.

[`moveSection(_:beforeSection:)`](/documentation/AppKit/NSDiffableDataSourceSnapshot-swift.struct/moveSection(_:beforeSection:))

Moves the section from its current position in the snapshot to the position immediately before the specified section.

### Reloading Data

[`reloadItems(_:)`](/documentation/AppKit/NSDiffableDataSourceSnapshot-swift.struct/reloadItems(_:))

Reloads the data within the specified items in the snapshot.

[`reloadSections(_:)`](/documentation/AppKit/NSDiffableDataSourceSnapshot-swift.struct/reloadSections(_:))

Reloads the data within the specified sections of the snapshot.

### Supporting Bridging

[`NSDiffableDataSourceSnapshotReference`](/documentation/AppKit/NSDiffableDataSourceSnapshotReference)

A representation of the state of the data in a view at a specific point in time.

## Relationships

### Conforms To

[`Escapable`](/documentation/Swift/Escapable)

[`Copyable`](/documentation/Swift/Copyable)

---

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)