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.
Follow these steps:
- Right-click the
JRNL
folder in the Project navigator and choose New File from Template... 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. Move the file so that it is under theSceneDelegate
file. - Add the following code after the
import
statement:import SwiftData
This lets you use the SwiftData framework.
- Add the following code after the
import
statements...