Core Data + Swift + load model not working

I generated the managed objects in Swift, not Objective-C. Do you know if Swift generation is not working?

I am getting 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 a data model to the project. What then?

+3


source to share


2 answers


This is not a good practice if you are moving a model from one project to another in Xcode. You will get this crash because the application is unable to load the model file.



If you need the exact same model to use in another project, then open the source model file in Finder, copy the relevant xml part, then open the target model file in Finder and skip that xml part.

+7


source


I had the same problem with my application. I wanted to use master data in an existing project that I had not initialized with the master data selected.

Make sure 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")!

      

+1


source







All Articles