我重新排列了程序文件夹中的文件,将它们分组到适当的子文件夹中。我确保它们都显示在编译的源列表中,包括“xcdatamodeld”文件。
但是,创建托管对象模型不适用于以下代码:
if (mom_ != nil) {
return mom_;
}
self.mom = [NSManagedObjectModel mergedModelFromBundles:nil];
return mom_;
我检查了[NSBundle mainBundle]
,特别是
[[NSBundle mainBundle] pathForResource:@"Words" ofType:@"xcdatamodeld"]
和
[[NSBundle mainBundle] pathForResource:nil ofType:@"xcdatamodeld"]
它们都返回零。当我按名称和类型检查其他资源时,我可以看到其他资源也在那里。
应用程序包文件中有一个名为“Words.momd”的文件夹。
可能发生了什么以及如何修复?
最佳答案
尝试使用:
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Words" withExtension:@"momd"];
您想要加载已编译的数据模型,而不是 xcdatamodeld 文件:
A data model is a deployment resource. In addition to details of the entities and properties in the model, a model you create in Xcode contains information about the diagram—its layout, colors of elements, and so on. This latter information is not needed at runtime. The model file is compiled using the model compiler, momc, to remove the extraneous information and make runtime loading of the resource as efficient as possible.
( source )
关于ios - 核心数据模型文件不在 bundle 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13403414/