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

# Document

A document that supports both reading and writing.

```
protocol Document : ReadableDocument, WritableDocument
```

## Overview

`Document` is a convenience protocol that combines
[`ReadableDocument`](/documentation/SwiftUI/ReadableDocument) and [`WritableDocument`](/documentation/SwiftUI/WritableDocument). Conform to it
when your document can both open and save files:

```
@Observable
final class TextDocument: Document {
    static let readableContentTypes = [UTType.plainText]

    var text: String = ""

    func reader(configuration: sending ReadConfiguration) -> sending FileWrapperDocumentReader<String> {
        FileWrapperDocumentReader(configuration) { fileWrapper in
            guard let data =
                fileWrapper.regularFileContents else {
                throw CocoaError(.fileReadCorruptFile)
            }
            return String(decoding: data, as: UTF8.self)
        }
    }

    func writer(configuration: sending WriteConfiguration) -> sending FileWrapperDocumentWriter<String> {
        FileWrapperDocumentWriter(configuration) { snapshot, _ in
            FileWrapper(
                regularFileWithContents: Data(snapshot.utf8)
            )
        }
    }

    @MainActor
    func snapshot(contentType: UTType) async throws -> sending String { text }

    @MainActor
    func apply(snapshot: sending String, previous: sending String?) async throws {
        text = snapshot
    }
}
```

Use [`DocumentGroup`](/documentation/SwiftUI/DocumentGroup) as your app’s first scene to opt into the
document infrastructure (autosaving, file coordination, undo
management, conflict resolution):

```
@main
struct MyApp: App {
    var body: some Scene {
        DocumentGroup { document in
            TextEditorView(document: document)
        } makeDocument: { configuration, context in
            TextDocument()
        }
    }
}
```

For a read-only document, conform only to [`ReadableDocument`](/documentation/SwiftUI/ReadableDocument).

The document can be `@MainActor` or nonisolated, `Sendable` or
not — use whichever works best for the app.

## Relationships

### Inherits From

[`ReadableDocument`](/documentation/SwiftUI/ReadableDocument)

[`WritableDocument`](/documentation/SwiftUI/WritableDocument)

---

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)