2

I often got a crash when moving model file from project to project. Any idea why? If I create it from Xcode, and create entity manually, it does not happen. Why?

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 = NSBundle.mainBundle().URLForResource("Model", withExtension: "momd")!
return NSManagedObjectModel(contentsOfURL: modelURL)!
}()

enter image description here

UPDATE

The file extension is not momd, but xcdatamodel. But that should be not a problem. Pervious project with Model and same name was working.

File is added to the target: enter image description here

and in console you can see it is exist in the right place: enter image description here

János
  • 32,867
  • 38
  • 193
  • 353

2 Answers2

1

I found this but it was not working. By the way it is something wrong with compilation. I removed model file, added a new empty model with previous naming but now from Xcode. And then edited the model file manually, and inserted the relating xml tags.

Community
  • 1
  • 1
János
  • 32,867
  • 38
  • 193
  • 353
0

The line where you crash tries to load a file named Model.momd from the app bundle and throws an exception ("unexpectedly found nil while unwrapping an Optional value") if that file doesn't exist

You're either

  • Using the wrong filename, i.e. your model file exists but is not named Model.momd, or
  • Using the right filename but forgetting to include this model file in the app's target when you add it to the new project. Meaning, the file exists in your Xcode project but you haven't told Xcode to actually include it when building the app.

You can check on the second possibility by selecting the uncompiled model file (Model.xcdatamodel in your case) in the file browser on the left side of Xcode's window, and then looking in the file inspector on the right, under "Target Membership". Make sure the app target is checked:

Xcode target membership

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • Both of your condition is satisfied and still got the crash. Strange. – János Jan 16 '15 at 21:09
  • Then the model doesn't exist in the app bundle for some other reason. Maybe you're copying over a broken copy and compilation fails for some reason. – Tom Harrington Jan 16 '15 at 22:10