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.
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
@Statevaluetextholds the document's contents and is bound through$textto aTextEditor, so every keystroke updates the same string the writer will later serialize.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'stextis wrapped in aFileWrapperand written to the document file, which is exactly the bytes the writer emits.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
FileWrapperDocumentWriterDemoview supplies the editable contents, and the framework's writer takes over from there to commit them to theNotes.txtfile theLabelnames.Choose the file-wrapper representation
Because the writer emits a
FileWrapperrather than rawData, 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 oftext, but the same path scales to bundle-style documents.
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.
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()
}
}