János
János

Reputation: 35038

Core Data + Swift + load model in does not work

I generated managed objects in Swift language so not in Objective-C anymore. Do you know if Swift generation is not working yet?

I get one error on this line:

let modelURL = NSBundle.mainBundle().URLForResource("TA", withExtension: "mom")

2014-10-12 18:17:13.940 TA[1767:555144] CoreData: warning: Unable to load class named 'Trip' for entity 'Trip'.  Class not found, using default NSManagedObject instead.

fatal error: unexpectedly found nil while unwrapping an Optional value

I have added data model to the project. What then?

Upvotes: 3

Views: 3421

Answers (2)

János
János

Reputation: 35038

It is not a good practice if you move model from one project to an other in Xcode. You will get this crash because app is unable to load model file.

If you need exact the same model to use in an other project as well, then open original model file in Finder, copy appropriate part of xml, then open target model file in Finder and past that xml part into.

Upvotes: 7

Connor Knabe
Connor Knabe

Reputation: 927

I had the same issue with my application. I wanted to use core data on an existing project that I didn't initialize with core data selected.

Make sure that you change the name of the Model in two places.

let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("TA.sqlite")

And

let modelURL = NSBundle.mainBundle().URLForResource("TA", withExtension: "momd")!

Upvotes: 1

Related Questions