WritableDocument
protocol · iOS 27.0+ · SDK 27.1
protocol WritableDocument : AnyObjectA document type that supports writing to file. Conform to `WritableDocument` to add save and export capabilities. Most documents also conform to ``ReadableDocument`` — use the ``Document`` protocol as a shorthand for both. The document saving has three steps: 1. SwiftUI calls ``snapshot(contentType:)`` on the main actor. 2. SwiftUI calls ``writer(configuration:)`` to get a writer. 3. The writer's ``DocumentWriter/write(content:to:previous:progress:)`` runs in the background with coordinated file access. > Important: Without registered undo actions, SwiftUI won't > trigger autosave. Register undo actions with the undo manager > from the `View` environment for every user-facing change. Example using ``FileWrapperDocumentWriter``: @Observable final class NoteDocument: WritableDocument { static let writableContentTypes: [UTType] = [.markdown] var text = "" 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 } } Register undo actions in the view using the environment's `UndoManager`. This ensures SwiftUI detects unsaved changes and triggers autosave: struct NoteEditorView: View { @Bindable var document: NoteDocument @Environment(\.undoManager) private var undoManager var body: some View { TextEditor(text: $document.text) .onChange(of: document.text) { oldValue, _ in undoManager?.registerUndo( withTarget: document ) { document in document.text = oldValue } } } }
Members (0)
This type has no directly callable members in this snapshot. See the framework’s skipped declarations for unsupported APIs.
Skipped declarations
No declarations were skipped in this snapshot.