TechnologiesSwiftUIDocuments

FileWrapperDocumentWriter struct

iOSmacOStvOSwatchOSvisionOS✓ renders

A document writer that uses `FileWrapper` for writing.

How it works

FileWrapperDocumentWriter is the structure SwiftUI uses to turn a document's in-memory contents into a FileWrapper and write it to disk. When a document-based app saves, exports, or autosaves, the framework needs a concrete writer that packages your model as the file-system representation the system can persist; FileWrapperDocumentWriter is that writer for documents whose payload is a file wrapper rather than a flat blob of data. You don't usually construct it yourself — SwiftUI creates it on your behalf and calls into your document type's write logic — but it is the piece that decides what bytes, and what wrapper, land on disk. Reach for an understanding of it whenever a view's editable state must round-trip to a file the user owns, especially documents that may carry more than a single stream of bytes.

  1. Back the editor with document state

    FileWrapperDocumentWriter reads its model whenever a save occurs, so you keep that model in view state and edit it normally. The @State value text holds the document's contents and is bound through $text to a TextEditor, so every keystroke updates the same string the writer will later serialize.

  2. Wrap contents in a FileWrapper at save time

    The defining job of FileWrapperDocumentWriter is to produce a FileWrapper — the Foundation representation of a file or directory — from your current model. Here the caption spells out the contract directly: on save, the editor's text is wrapped in a FileWrapper and written to the document file, which is exactly the bytes the writer emits.

  3. Let the write happen through the document infrastructure

    You don't invoke FileWrapperDocumentWriter by hand; SwiftUI drives it through the document scene that hosts this view, calling your write logic when the document is saved, exported, or autosaved. The FileWrapperDocumentWriterDemo view supplies the editable contents, and the framework's writer takes over from there to commit them to the Notes.txt file the Label names.

  4. Choose the file-wrapper representation

    Because the writer emits a FileWrapper rather than raw Data, it can represent a single regular file or a directory package whose pieces are written together. In this example the document is a single text file, so the writer produces one regular-file wrapper holding the UTF-8 contents of text, but the same path scales to bundle-style documents.

Try it — Change the bound text initial value in @State private var text to a multi-line string and save — the file FileWrapperDocumentWriter writes will contain exactly those edited lines, while the on-screen TextEditor keeps showing what you typed.

Example & preview

Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.

FileWrapperDocumentWriter.swift
struct FileWrapperDocumentWriterDemo: View {
    @State private var text = "Hello, file wrapper.\nEdited contents are written back to disk."

    var body: some View {
        VStack(alignment: .leading, spacing: 12) {
            Label("Notes.txt", systemImage: "doc.text")
                .font(.headline)
            TextEditor(text: $text)
                .frame(height: 140)
                .overlay(RoundedRectangle(cornerRadius: 8).stroke(.gray.opacity(0.4)))
            Text("On save, contents are wrapped in a FileWrapper and written to the document file.")
                .font(.caption)
                .foregroundStyle(.secondary)
        }
        .padding()
    }
}
Live preview
Notes.txt Hello, file wrapper.\nEdited contents are written back to disk. On save, contents are wrapped in a FileWrapper and written to the document file.
Notes.txt Hello, file wrapper.\nEdited contents are written back to disk. On save, contents are wrapped in a FileWrapper and written to the document file.
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →