<!--
{
  "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/DocumentWriter",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI14DocumentWriterP"
  },
  "title" : "DocumentWriter"
}
-->

# DocumentWriter

A type that writes a document’s content to a file.

```
protocol DocumentWriter<Snapshot>
```

## Overview

SwiftUI calls your document’s
[`snapshot(contentType:)`](/documentation/SwiftUI/WritableDocument/snapshot(contentType:)) on the main actor to
capture the current state, then obtains a `DocumentWriter` from
[`writer(configuration:)`](/documentation/SwiftUI/WritableDocument/writer(configuration:)) and invokes
`write(content:to:previous:progress:)` in the background
with coordinated file access.

Use [`FileWrapperDocumentWriter`](/documentation/SwiftUI/FileWrapperDocumentWriter) for cases cases that don’t require
custom file write logic. Implement a
`DocumentWriter` when you need direct URL access or
streaming writes:

```
struct ImageWriter: DocumentWriter {
    @concurrent
    func write(content image: sending CGImage, to destination: URL,
        previous: sending CGImage?, progress: consuming Subprogress
    ) async throws {
        guard let imageDestination =
            CGImageDestinationCreateWithURL(
                destination as CFURL,
                UTType.jpeg.identifier as CFString,
                1, nil
            ) else {
            throw CocoaError(.fileWriteUnknown)
        }
        CGImageDestinationAddImage(
            imageDestination, image, nil
        )
        guard CGImageDestinationFinalize(
            imageDestination
        ) else {
            throw CocoaError(.fileWriteUnknown)
        }
    }
}
```

## Topics

### Writing a document

[`write(snapshot:to:previous:progress:)`](/documentation/SwiftUI/DocumentWriter/write(snapshot:to:previous:progress:))

Writes the document content to disk.

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

The type representing the document’s content to write.

[`Destination`](/documentation/SwiftUI/DocumentWriter/Destination)

The type of the destination location to write to.



---

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)