Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

iphone - How to create the magic .xcdatamodeld folder / package?

Recently, I managed (by accident, don't ask) to delete my Core Data datamodel files and classes. And I was completely and utterly unable to create the files again to be exactly the same as they would be in a freshly started project where those files are being prepared automatically by XCode.

In a new project, the files would look like this:

alt text

while all I was able to create was:

alt text

Did I mention that the app didn't work anymore this way? It crashed saying the model must not be nil. The problem was solved by starting a new project with the same name and dragging the files over to the old one - not the most elegant solution, I suppose.

I figured however that there are things about Core Data that I still don't quite understand, obviously.

Please, enlighten me - what's the magic behind the .xcdatamodeld folder? Why would Foo.xcdatamodel feature that green checked-icon? Does the datamodel need to be compiled or processed in some way?

Thanks alot guys!!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

the coredata model bundle keeps all your model versions. The model with the green checkmark is the current version, data stored into coredata will be saved using this scheme. All other datamodel versions are there to support database migration, data stored in a different version can be upgraded automatically if there are only small changes in the data model.

To create a model bundle you select the single model, open the Design menu in the mainmenu and select Data Model and Add Model Version.

Your app is crashing because it expects a datamodel bundle with the type momd.

NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"foobar" ofType:@"momd"];

If you have only one Datamodel it is not compiled into a "momd"-file. But I forgot the actual extension.


In Xcode4 Add Model Version can be found within the Editor main menu.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...