Contents

fileWrapper(snapshot:configuration:)

Serializes a document snapshot to a file wrapper.

Declaration

func fileWrapper(snapshot: Self.Snapshot, configuration: Self.WriteConfiguration) throws -> FileWrapper

Parameters

  • snapshot:

    The document snapshot to save.

  • configuration:

    Information about a file that already exists for the document, if any.

Return Value

The destination to serialize the document contents to. The value can be a newly created FileWrapper or an update of the one provided in the configuration input.

Discussion

To store a document — for example, in response to a Save command — SwiftUI begins by calling the snapshot(contentType:) method to get a copy of the document data in its current state. Then SwiftUI passes that snapshot to this method, where you serialize it and create or modify a file wrapper with the serialized data:

func fileWrapper(snapshot: Snapshot, configuration: WriteConfiguration) throws -> FileWrapper {
    let data = try JSONEncoder().encode(snapshot)
    return FileWrapper(regularFileWithContents: data)
}

SwiftUI disables document edits during the snapshot to ensure that the document’s data remains coherent, but reenables edits during the serialization operation.

See Also

Writing a document