How it works
DocumentBaseBox is the protocol that defines the underlying storage box SwiftUI uses to hold a document's value while it is being read from or written to disk. When a document-based scene loads a file, the framework needs a type-erased container that can carry the decoded contents, track edits, and hand them back for serialization without the scene having to know the concrete document type. DocumentBaseBox describes that container's base interface, sitting beneath the FileDocument and ReferenceFileDocument machinery. It is part of SwiftUI's document-management plumbing rather than something you construct directly, so you encounter it through the document APIs that build on top of it rather than by conforming to it yourself.
Understand the box as the document's storage backing
A DocumentBaseBox is the wrapper that holds the live document value between a read and a write. SwiftUI creates and owns one for each open document so that the editing surface, such as the
TextEditor(text: $text)shown here, has a stable place to push changes into. The box is what keeps that edited state coupled to the file the scene is managing.Reach the box through DocumentGroup and the document scene
You never allocate a DocumentBaseBox; you opt into it by declaring a document scene. A
DocumentGrouppaired with a FileDocument type causes SwiftUI to provision the base box behind the scenes and bind it to your document's editable value. The standalone view in this example, with itsLabel("Untitled.txt", systemImage: "doc.text")heading, illustrates the editing surface such a scene would present once the box is in place.Read and write through the box's contents
The box exposes the document value so the framework can pull it out for serialization and put a freshly decoded value back in when a file loads. Edits made in the editor, here flowing through the
@State private var textbinding, are the kind of mutations a DocumentBaseBox preserves so that a later save writes the current contents rather than the original file data.Treat it as a base protocol, not a conformance target
DocumentBaseBox sits at the bottom of the document-storage hierarchy and is refined by more specific box types that the framework selects based on whether your document is a value type or a reference type. Because it is base infrastructure, your code interacts with the document through higher-level protocols and modifiers; the box itself remains an implementation detail that the
bodyof a document view, like theVStackof editor chrome above, never names.
@State private var text initializer from "# Hello, Document\n\nEdit me…" to your own starting contents and watch that value become the document's initial state the storage box would carry into the first save.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 DocumentBaseBoxDemo: View {
@State private var text = "# Hello, Document\n\nEdit me…"
var body: some View {
VStack(alignment: .leading, spacing: 8) {
Label("Untitled.txt", systemImage: "doc.text")
.font(.headline)
TextEditor(text: $text)
.font(.body.monospaced())
.frame(height: 140)
.overlay(RoundedRectangle(cornerRadius: 8).stroke(.secondary))
}
.padding()
}
}