<!--
{
  "availability" : [
    "iOS: 14.0.0 - 27.0.0",
    "iPadOS: 14.0.0 - 27.0.0",
    "macCatalyst: 14.0.0 - 27.0.0",
    "macOS: 11.0.0 - 27.0.0",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/ReferenceFileDocument",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI21ReferenceFileDocumentP"
  },
  "title" : "ReferenceFileDocument"
}
-->

# ReferenceFileDocument

A type that you use to serialize reference type documents to and from file.

```
@preconcurrency protocol ReferenceFileDocument : ObservableObject, Sendable
```

## Overview

To store a document as a reference type — like a class — create a type
that conforms to the `ReferenceFileDocument` protocol and implement the
required methods and properties. Your implementation:

- Provides a list of the content types that the document can read from and
  write to by defining [`readableContentTypes`](/documentation/SwiftUI/ReferenceFileDocument/readableContentTypes). If the list of content
  types that the document can write to is different from those that it reads
  from, you can optionally also define [`writableContentTypes`](/documentation/SwiftUI/ReferenceFileDocument/writableContentTypes).
- Loads documents from file in the [`init(configuration:)`](/documentation/SwiftUI/ReferenceFileDocument/init(configuration:)) initializer.
- Stores documents to file by providing a snapshot of the document’s
  content in the [`snapshot(contentType:)`](/documentation/SwiftUI/ReferenceFileDocument/snapshot(contentType:)) method, and then serializing
  that content in the [`fileWrapper(snapshot:configuration:)`](/documentation/SwiftUI/ReferenceFileDocument/fileWrapper(snapshot:configuration:)) method.

Ensure that types that conform to this protocol are `Sendable`.
In particular, SwiftUI calls the protocol’s methods from different isolation domains.
Don’t perform serialization and deserialization on `MainActor`.

```
final class PDFDocument: ReferenceFileDocument {
    struct Storage {
        var contents: Data
    }

    static let readableContentTypes: [UTType] = [.pdf]
    let storage: Mutex<Storage>

    required init(configuration: ReadConfiguration) throws {
       guard let data = configuration.file.regularFileContents else {
           throw CocoaError(.fileReadCorruptFile)
       }
        self.storage = .init(.init(contents: data))
    }

    func snapshot(contentType: UTType) throws -> Data {
        storage.withLock { $0.contents }
    }

    func fileWrapper(snapshot: Data, configuration: WriteConfiguration) throws -> FileWrapper {
        return FileWrapper(regularFileWithContents: snapshot)
    }
}
```

> Important: If you store your document as a value type — like a
> structure — use ``doc://com.apple.SwiftUI/documentation/SwiftUI/FileDocument`` instead.

## Topics

### Reading a document

[`init(configuration:)`](/documentation/SwiftUI/ReferenceFileDocument/init(configuration:))

Creates a document and initializes it with the contents of a file.

[`readableContentTypes`](/documentation/SwiftUI/ReferenceFileDocument/readableContentTypes)

The file and data types that the document reads from.

[`ReferenceFileDocument.ReadConfiguration`](/documentation/SwiftUI/ReferenceFileDocument/ReadConfiguration)

The configuration for reading document contents.

### Getting a snapshot

[`snapshot(contentType:)`](/documentation/SwiftUI/ReferenceFileDocument/snapshot(contentType:))

Creates a snapshot that represents the current state of the document.

[`Snapshot`](/documentation/SwiftUI/ReferenceFileDocument/Snapshot)

A type that represents the document’s stored content.

### Writing a document

[`fileWrapper(snapshot:configuration:)`](/documentation/SwiftUI/ReferenceFileDocument/fileWrapper(snapshot:configuration:))

Serializes a document snapshot to a file wrapper.

[`writableContentTypes`](/documentation/SwiftUI/ReferenceFileDocument/writableContentTypes)

The file types that the document supports saving or exporting to.

[`ReferenceFileDocument.WriteConfiguration`](/documentation/SwiftUI/ReferenceFileDocument/WriteConfiguration)

The configuration for writing document contents.



---

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)