One Logo Pool Ball
SwiftUI · IOS API REFERENCE

ReferenceFileDocument

protocol · iOS 14.0+ · Deprecated in iOS 100000.0 · SDK 27.1

@preconcurrency protocol ReferenceFileDocument : ObservableObject, Sendable

A type that you use to serialize reference type documents to and from file. To store a document as a reference type --- like a class --- create a type that conforms to the `ReferenceFileDocument` protocol and implement the required methods and properties. Your implementation: * Provides a list of the content types that the document can read from and write to by defining ``readableContentTypes``. If the list of content types that the document can write to is different from those that it reads from, you can optionally also define ``writableContentTypes-6x6w9``. * Loads documents from file in the ``init(configuration:)`` initializer. * Stores documents to file by providing a snapshot of the document's content in the ``snapshot(contentType:)`` method, and then serializing that content in the ``fileWrapper(snapshot:configuration:)`` method. Because `ReferenceFileDocument` is a reference type, SwiftUI can't detect changes to your document's properties on its own. Register an undo action with the environment's `UndoManager` whenever you mutate the document, for example by calling ``UndoManager/registerUndo(withTarget:handler:)``. Ensure that types that conform to this protocol are ``Sendable``. In particular, SwiftUI calls the protocol's methods from different isolation domains. Don't perform serialization and deserialization on ``MainActor``. final class PDFDocument: ReferenceFileDocument { struct Storage { var contents: Data } static let readableContentTypes: [UTType] = [.pdf] let storage: Mutex<Storage> required init(configuration: ReadConfiguration) throws { guard let data = configuration.file.regularFileContents else { throw CocoaError(.fileReadCorruptFile) } self.storage = .init(.init(contents: data)) } func snapshot(contentType: UTType) throws -> Data { storage.withLock { $0.contents } } func fileWrapper(snapshot: Data, configuration: WriteConfiguration) throws -> FileWrapper { return FileWrapper(regularFileWithContents: snapshot) } } > Important: If you store your document as a value type --- like a structure --- use ``FileDocument`` instead. Because a value type's changes are always visible to SwiftUI, undo management and the registration of undo actions are automatic.

Members (0)

This type has no directly callable members in this snapshot. See the framework’s skipped declarations for unsupported APIs.

Skipped declarations

1 declarations were excluded from these bindings. Each reason comes from the generator or the iOS SDK compiler.

ReferenceFileDocument.writableContentTypes

Static protocol requirements cannot be called through an instance.

All SwiftUI types →