Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 721
János Avatar asked Feb 03 '26 04:02

János


2 Answers

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.

like image 81
János Avatar answered Feb 04 '26 16:02

János


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")!
like image 42
Connor Knabe Avatar answered Feb 04 '26 16:02

Connor Knabe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!