Contents

init(newDocument:editor:)

Creates a document group for creating and editing file documents.

Declaration

@preconcurrency nonisolated init(newDocument: @autoclosure  @escaping  @Sendable () -> Document, @ContentBuilder editor: @escaping (FileDocumentConfiguration<Document>) -> Content)

Parameters

  • newDocument:

    The initial document to use when a user creates a new document.

  • editor:

    The editing UI for the provided document.

Discussion

Use a DocumentGroup scene to tell SwiftUI what kinds of documents your app can open when you declare your app using the App protocol. You initialize a document group scene by passing in the document model and a view capable of displaying the document’s contents. The document types you supply to DocumentGroup must conform to FileDocument or ReferenceFileDocument. SwiftUI uses the model to add document support to your app. In macOS this includes document-based menu support including the ability to open multiple documents. On iOS this includes a document browser that can navigate to the documents stored on the file system and multiwindow support:

@main
struct MyApp: App {
    var body: some Scene {
        DocumentGroup(newDocument: TextFile()) { file in
            ContentView(document: file.$document)
        }
    }
}

The document types you supply to DocumentGroup must conform to FileDocument or ReferenceFileDocument. Your app can support multiple document types by adding additional DocumentGroup scenes.

With the newDocument: initializer, SwiftUI considers your app as an editor of documents of given content types (FileDocument.writableContentTypes). On macOS, this adds a File > New menu item and enables the standard document commands.

On iOS, it shows a New Document button in the document browser. The isEditable property is true. Use the DocumentGroup/init(viewing:editor:) initializer instead if your app should only display documents without modifying them.

See Also

Creating a document group