Implementing SwiftData components
Now that you have used the JournalEntry
class to create a JournalEntry
model, you will create a singleton class that contains a ModelContainer
object and a ModelContext
instance. You will then add methods to manipulate instances of the JournalEntry
model stored in ModelContext
.
The term singleton means that there is only one instance of this class in your app, and this is also applicable when you create your own apps that use SwiftData.
Follow these steps:
- Right-click the JRNL folder in the Project navigator and choose New File... from the pop-up menu.
- iOS should already be selected. Choose Swift File and then click Next.
- Name this file
SharedData
. Click Create. TheSharedData
file appears in the Project navigator. - Add the following code after the
import
statement:import SwiftData
This lets you use the SwiftData framework.
- Add the following code after the
import
...