Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone - Add Core Data To existing Project?

I am trying to add core data functionality to an existing project. I added a data model file named "myProj.xcdatamodel"

My code crashes in the following when getting the managedObjectModel What is the "momd" file? where can i get it or how can i create it? When i read the path it returns null and crashes the app.

NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"myProj" ofType:@"momd"];
NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
like image 461
aryaxt Avatar asked Oct 11 '10 18:10

aryaxt


People also ask

Can you add Core Data to an existing project?

Get our help adding Core Data to your project So, with your existing project open, create a new project in Xcode (⇧⌘N) and select a Single View App, you can call it whatever you like as we'll be deleting it when we're done. You'll see the “Use Core Data” checkbox on the project options screen, make sure it is checked.

What is Core Data stack in iOS?

Overview. After you create a data model file as described in Creating a Core Data Model, set up the classes that collaboratively support your app's model layer. These classes are referred to collectively as the Core Data stack.


1 Answers

The momd file is the versioned equivalent of the mom file. You have two options at this point:

  1. Add a version to your existing myProj.xcdatamodel model. In Xcode select the myProj.xcdatamodel file and select Design -> Data Model -> Add Model Version from the menu in Xcode.
  2. Only use the non versioned model file. Change your supplied code to:

    NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"myProj" ofType:@"mom"]; NSURL *modelURL = [NSURL fileURLWithPath:modelPath];

If you create a Navigation-based application (Use Core Data for storage) from the default template in Xcode, you'll notice the model file is already versioned.

For further information see: Model Versions

like image 129
Chris Gummer Avatar answered Oct 03 '22 21:10

Chris Gummer