ios - 核心数据模型文件不在 bundle 中

标签 ios core-data nsbundle

我重新排列了程序文件夹中的文件,将它们分组到适当的子文件夹中。我确保它们都显示在编译的源列表中,包括“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/

相关文章:

ios - 我如何在 CloudKit 中创建一个记录作为 child 的记录列表?

swift - 如何快速理解设置为私有(private)的全局变量?

objective-c - 后台线程和主线程同时访问核心数据时应用程序崩溃

ios - 应用程序包中的只读 SQLite 数据库

ios - NSBundle.mainBundle().pathForResource 返回 nil

ios - 如何将粘贴板中的图像粘贴到 UITextView 上?

iphone - 用于 ios 的 Spotify API : download , 保存,从 ios spotify api 访问轨道

iphone - 带有 bool 值的 AFNetworking JSON 请求

swift - 核心数据设置中 Appdelegate 约定中的 NsPerstentContainer 名称

iOS 扩展和访问资源中的文件