Overview 概要
This sample creates a checklist document that can have one or more checklist items. The user can select and deselect the checkboxes of items in the list, add and delete items, and rearrange items. The sample implements a Document
scene, and adopts the Reference
protocol.
Configure the Sample Code Project 見本のコードプロジェクトを構成設定する
To build and run this sample on your device, you must first select your development team for the project’s target using these steps:
Open the sample with the latest version of Xcode.
Select the top-level project.
For the project’s target, choose your team from the Team drop-down menu in the Signing & Capabilities pane to let Xcode automatically manage your provisioning profile.
Create the Data Model
This sample is a simple checklist app that keeps track of one or more items in a checklist, and whether the checkboxes of the items are in a selected state. The app has a data model that defines Checklist
and Checklist
objects, and these objects conform to Codable
to enable easy serialization. They also conform to Identifiable
for unique identification during enumeration.
Export the App’s Document Type
The app defines and exports a custom checklist document type that tells the operating system to open checklist documents with this app. The app does this by including an entry in its Information Property List
file under the CFBundle
and UTExported
keys. Additionally, the sample defines the bundle’s exported type as a UTType
to support the sample app’s data format.
For more information, see Defining File and Data Types for Your App.
Define the App’s Scene
A document-based SwiftUI app returns a Document
scene from its body
property. The new
parameter that an app supplies to the document group’s init(new
initializer must conform to File
or Reference
. This sample adopts Reference
. The trailing closure of the initializer returns a view that renders the document’s contents.
Adopt the Reference File Document Protocol
The sample’s Checklist
structure adopts the Reference
protocol to serialize checklists to and from files. This sample implements the required properties and methods to conform to the protocol. The readable
property defines the types that the sample can read, namely, the .checklist
type.
The sample reads data from a document in the init(configuration:)
method. After reading the file’s data, the initializer uses a JSONDecoder
to deserialize the data into model objects.
When the user saves the document, the sample returns a snapshot of the document’s data for serialization in the snapshot(content
instance method.
Conversely, in the file
method, a JSONEncoder
instance serializes the data model into a File
object that represents the data in the file system.
Register Undo and Redo Actions
In an app that uses File
for its document object, undo management and the registration of undo actions are automatic. However, because this sample uses a Reference
document class, the sample itself must perform undo management. Implementing undo management also alerts SwiftUI when the document changes. The Undo
class handles undo management, and SwiftUI supplies an instance of this class in the environment.
In this sample, the SwiftUI views handle user actions by calling the Checklist
and passing along the Undo
object.
The Checklist
toggles the is
state of the Checklist
, and registers an undo action that calls itself. This way, the sample doesn’t need to register a redo action when performing an undo action.