How it works
FileDocumentConfiguration is the value SwiftUI hands a document scene so its content view can read and write the open file. When you build a document app with DocumentGroup, the framework wraps your FileDocument in this structure and gives you a binding to the live document along with metadata about where it lives on disk and whether it may be changed. Reach for it whenever a view needs to edit the document's model directly or react to the file's location and editability, rather than just displaying a static copy.
Bind to the live document through document
The
documentproperty exposes the open file's model so edits flow straight back into the document SwiftUI is managing. Because it is projected as a binding, you reach a field of the model with the dollar projection — hereconfig.$document.textfeeds thetextbinding directly into aTextEditor, so typing mutates the document in place.Read the file's location from fileURL
fileURLis an optionalURLgiving the document's on-disk location, ornilfor a new, unsaved document. The example surfaces it for display withconfig.fileURL?.lastPathComponent ?? "Untitled", falling back to a placeholder when the file has no location yet.Check write access with isEditable
The
isEditableBoolean reports whether the document can currently be modified, which lets a view adapt its interface for read-only contexts such as a locked or shared file. Here it drives a status label viaconfig.isEditable ? "Editable" : "Read-only".Supply the FileDocument the configuration wraps
FileDocumentConfigurationis generic over aFileDocument, and the conforming type defines how bytes turn into a model and back. The example'sTextFiledeclaresreadableContentTypes, decodesconfiguration.file.regularFileContentsin itsReadConfigurationinitializer, and serializes throughfileWrapper(configuration:)so the structure has something concrete to expose.Obtain it from the document scene
In a real app you do not construct this yourself;
DocumentGroupbuilds it per open file and passes it to your content view. The demo instead stands one up directly withFileDocumentConfiguration(document:fileURL:)and a.constantbinding so the surface can be exercised in isolation.
fileURL argument to nil and watch the header fall back to "Untitled" through the lastPathComponent ?? "Untitled" expression.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 FileDocumentConfigurationDemo: View {
struct TextFile: FileDocument {
static var readableContentTypes: [UTType] { [.plainText] }
var text: String
init(text: String = "Hello") { self.text = text }
init(configuration: ReadConfiguration) throws {
let data = configuration.file.regularFileContents ?? Data()
text = String(decoding: data, as: UTF8.self)
}
func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
FileWrapper(regularFileWithContents: Data(text.utf8))
}
}
@State private var config = FileDocumentConfiguration(document: .constant(TextFile(text: "Draft notes")), fileURL: URL(fileURLWithPath: "/Documents/notes.txt"))
var body: some View {
VStack(alignment: .leading, spacing: 12) {
Text(config.fileURL?.lastPathComponent ?? "Untitled")
.font(.headline)
TextEditor(text: config.$document.text)
.frame(height: 120)
.border(.secondary)
Text(config.isEditable ? "Editable" : "Read-only")
.font(.caption)
.foregroundStyle(.secondary)
}
.padding()
}
}