iPhone - 将核心数据添加到现有项目? [英] iPhone - Add Core Data To existing Project?
问题描述
我正在尝试向现有项目添加核心数据功能。
我添加了一个名为myProj.xcdatamodel的数据模型文件
I am trying to add core data functionality to an existing project. I added a data model file named "myProj.xcdatamodel"
我的代码在获取managedObjectModel时崩溃
什么是 文件?在哪里可以得到它或如何创建它?
当我读取的路径,它返回null并崩溃的应用程序。
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];
推荐答案
momd文件是mom文件。此时有两个选项:
The momd file is the versioned equivalent of the mom file. You have two options at this point:
- 在现有的myProj.xcdatamodel模型中添加一个版本。在Xcode中选择myProj.xcdatamodel文件并从Xcode菜单中选择Design - > Data Model - > Add Model Version。
-
只使用非版本化的模型文件。将您提供的代码更改为:
- 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.
Only use the non versioned model file. Change your supplied code to:
NSString * modelPath = [[NSBundle mainBundle] pathForResource:@myProjofType:@mom NSURL * modelURL = [NSURL fileURLWithPath:modelPath];
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
这篇关于iPhone - 将核心数据添加到现有项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!