Adding Core Data to an existing application
When you create a new project in Xcode, Xcode asks whether you want to add Core Data to your application. If you check this checkbox, Xcode will automatically generate some boilerplate code that sets up the Core Data stack. For practicing purposes, MustC was set up without Core Data so you'll have to add this to the project yourself. Start by opening AppDelegate.swift
and add the following import
statement:
import CoreData
Next, add the following lazy
variable to the implementation of AppDelegate
:
private lazy var persistentContainer: NSPersistentContainer = { let container = NSPersistentContainer(name: "MustC") container.loadPersistentStores(completionHandler: { (storeDescription, error) in if let error = error { fatalError("Unresolved error (error), (error.userInfo)") } }) return container }()
Note
If you declare a variable as lazy
, it won't be initialized until it is accessed. This is particularly...