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
ModelContextassociated with the new document. Use this closure to set the document’s initial contents before it is displayed: insert preconfigured models in the providedModelContext.
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")
}