I recently updated a project after not touching it for a year or two, and now I'm having some issues.
Everything worked before and I even went thru each error to update the new syntax, but I'm stuck at one function in AppDelegate.Swift that's preventing me from running the program. It's this one here:
lazy var managedObjectModel: NSManagedObjectModel = {
// The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
let modelURL = Bundle.mainBundle.url(forResource: "ProjectName", withExtension: "momd")
return NSManagedObjectModel(contentsOfURL: modelURL ?? <#default value#>)!
}()
After I take the suggestions from Xcode and update the Syntax it looks like this:
lazy var managedObjectModel: NSManagedObjectModel = {
// The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
let modelURL = Bundle.main.url(forResource: "ProjectName", withExtension: "momd")
return NSManagedObjectModel(contentsOf: modelURL ?? <#default value#>)!
}()
I'm really confused as to what to put for the default value as I've never edited or touched AppDelegate before. What do I need to put here?
Thank you for any help you may provide!
CodePudding user response:
As the application doesn't work at all if the model is missing you can force unwrap the URL
lazy var managedObjectModel: NSManagedObjectModel = {
// The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
let modelURL = Bundle.main.url(forResource: "ProjectName", withExtension: "momd")!
return NSManagedObjectModel(contentsOf: modelURL)!
}()
Consider that since iOS 10, macOS 10.12 there is a more convenient way to initialize the Core Data stack with NSPersistentContainer