xcode 如何创建神奇的 .xcdatamodeld 文件夹/包?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4518000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:02:18  来源:igfitidea点击:

How to create the magic .xcdatamodeld folder / package?

iphoneobjective-cxcodecore-data

提问by Toastor

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.

最近,我设法(偶然地,不要问)删除了我的 Core Data 数据模型文件和类。而且我完全无法再次创建文件,使其与 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.

我有没有提到该应用程序不再以这种方式工作?它崩溃了,说模型一定不是nil。问题是通过启动一个同名的新项目并将文件拖到旧项目来解决的 - 我想这不是最优雅的解决方案。

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

然而,我认为关于 Core Data 的一些事情我仍然不太明白,很明显。

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?

请赐教 - .xcdatamodeld 文件夹背后的魔法是什么?为什么 Foo.xcdatamodel 会有那个绿色的勾选图标?数据模型是否需要以某种方式编译或处理?

Thanks alot guys!!!

非常感谢各位!!!

回答by Matthias Bauch

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.

coredata 模型包保留所有模型版本。带有绿色勾号的模型是当前版本,存储到 coredata 中的数据将使用此方案保存。所有其他版本的数据模型都支持数据库迁移,存储在不同版本中的数据如果数据模型只有很小的变化就可以自动升级。

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

要创建模型包,请选择单个模型,打开主Design菜单中的菜单并选择Data ModelAdd Model Version

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

您的应用程序崩溃,因为它需要类型为 的数据模型包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.

如果您只有一个 Datamodel,则它不会编译为“momd”文件。但我忘记了实际的扩展名。



In Xcode4 Add Model Versioncan be found within the Editormain menu.

在Xcode4Add Model VersionEditor主菜单内可以找到。

回答by Luc Bloom

During my development, I could not find Entities that I added later on. What worked for me:

在我的开发过程中,我找不到后来添加的实体。什么对我有用:

Uninstall the App EVERY TIME you change the Data Model!

每次更改数据模型时都卸载应用程序!

The Data Model is cached by Core Data between installs, to make sure the integrity stays in tact. Delete the App from the simulator / iPhone to be able to test your changes.

数据模型在安装之间由 Core Data 缓存,以确保完整性保持不变。从模拟器/iPhone 中删除应用程序,以便能够测试您的更改。

PS: does anyone know how to do that automatically?

PS:有谁知道如何自动做到这一点?