Contents

init(editing:contentType:editor:prepareDocument:)

Instantiates a document group for creating and editing documents that store a specific model type.

Declaration

init(editing modelType: any PersistentModel.Type, contentType: UTType, editor: @escaping () -> Content, prepareDocument: @escaping (ModelContext) -> Void = { _ in })

Parameters

  • modelType:

    The model type defining the schema used for each document.

  • contentType:

    The content type of the document. It should conform to UTType.package.

  • editor:

    The editing UI for the provided document.

  • prepareDocument:

    The optional closure that accepts ModelContext associated with the new document. Use this closure to set the document’s initial contents before it is displayed: insert preconfigured models in the provided ModelContext.

Discussion

 @main
 struct Todo: App {
     var body: some Scene {
         DocumentGroup(editing: TodoItem.self, contentType: .todoItem) {
             ContentView()
         }
     }
 }

 struct ContentView: View {
     @Query var items: [TodoItem]

         var body: some View {
             List {
                 ForEach(items) { item in
                     @Bindable var item = item
                     Toggle(item.text, isOn: $item.isDone)
                 }
              }
         }
 }

 @Model
 final class TodoItem {
     var created: Date
     var text: String
     var isDone = false
 }

 extension UTType {
     static var todoItem = UTType(exportedAs: "com.myApp.todoItem")
 }

See Also

Editing a document backed by a persistent store