<!--
{
  "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/write(snapshot:to:previous:progress:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI14DocumentWriterP5write8snapshot2to8previous8progressy8SnapshotQzn_11DestinationQznAJSgn10Foundation11SubprogressVntYaKF"
  },
  "title" : "write(snapshot:to:previous:progress:)"
}
-->

# write(snapshot:to:previous:progress:)

Writes the document content to disk.

```
@concurrent func write(snapshot: sending Self.Snapshot, to destination: sending Self.Destination, previous: sending Self.Snapshot?, progress: consuming Subprogress) async throws
```

## Parameters

`snapshot`

The snapshot to write to disk.

`destination`

The file URL to write to.

`previous`

The last successfully written snapshot, or
`nil` on the first save. Compare to `content` to write
only what changed in package documents.

`progress`

A `Subprogress` value to report writing
progress. Consume it once with `reporter(totalCount:)`
and call `complete(count:)` as units finish.

## Discussion

SwiftUI calls this method in the background after
obtaining a snapshot via
[`snapshot(contentType:)`](/documentation/SwiftUI/WritableDocument/snapshot(contentType:)). Perform all
serialization and disk access here.

For most documents, use [`FileWrapperDocumentWriter`](/documentation/SwiftUI/FileWrapperDocumentWriter) instead
of implementing a custom writer. Only implement `write`
yourself when you need capabilities `FileWrapperDocumentWriter`
doesn’t provide — such as direct URL access for Core Graphics,
AVFoundation, or other frameworks that operate on file paths:

```
@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)
    }
}
```

---

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)