Swift, core data, Bundle.main.url error

In my coreDataStack if have the following:

 private lazy var managedObjectModel: NSManagedObjectModel = {
 let modelURL = Bundle.main.url(forResource: self.modelName, withExtension: "momd")!
 return NSManagedObjectModel(contentsOf: modelURL)!
 }()

When the app is run I get a crash on the letModelURL line, fatal error: unexpectedly found nil while unwrapping an Optional value.

This app is an upgrade from an Objective C version and I want to be sure that users can still access their data. Any ideas why I am getting the crash?

The code’s expecting ‘Bundle.main.url()’ to return a value (with the ! meaning the app should crash if the function unexpectedly returns nil). I would double check: what’s the value of ‘modelName’ and does the project have a resource with that name of type ‘.momd’?

I would agree that logic suggests that, I do have a question though, why did it work with Swift 2, but not with Swift 3? The type “momd” is boiler plate provided by Apple, so the Core Data app should be expecting it and not all data bases come pre-loaded.