更新到 Xcode 6.0 后,创建 NSManagedObject 子类不生成属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25853366/
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-15 05:33:36  来源:igfitidea点击:

Create NSManagedObject subclass not generating property after updating to Xcode 6.0

xcode

提问by dcep1

After updating to Xcode Version 6.0 (6A313), when I add a new attribute to my xcdatamodeld entry, "Create NSManagedObject subclass" is not adding the new property to the generated header file. This works fine after closing Xcode 6, and then running Version 5.1.1 (5B1008). I repeated this process a couple of times with the same results. Do I need to do something to allow a new Xcode version to properly manage my data models, or recreate them, or is this possibly just a bug with the new Xcode?

更新到 Xcode 版本 6.0 (6A313) 后,当我向 xcdatamodeld 条目添加新属性时,“创建 NSManagedObject 子类”不会将新属性添加到生成的头文件中。在关闭 Xcode 6,然后运行版本 5.1.1 (5B1008) 后,这可以正常工作。我重复了几次这个过程,结果相同。我是否需要做一些事情来允许新的 Xcode 版本正确管理我的数据模型,或重新创建它们,或者这可能只是新 Xcode 的一个错误?

Here are the specific steps I took:
1. Open DataModel.xcdatamodeld in Xcode 6.0 (GM Seed).
2. Select correct entity
3. Click + to add new attribute
4. Enter name and selected Boolean type for new attribute
5. Editor->Create NSManagedObject subclass
6. Ensure proper data model is checked, click next
7. Ensure correct entity is checked, click next
8. Choose location to save (using root project folder), select Create
9. Choose "Replace" in dialogue box
10. Look for variable in generated header file. Variable not present.
11. Close Xcode6
12. Repeat steps 1-10 in Xcode 5.1.1, worked correctly
13. Repeated this process twice to verify same results

以下是我采取的具体步骤:
1. 在 Xcode 6.0 (GM Seed) 中打开 DataModel.xcdatamodeld。
2. 选择正确的实体
3. 单击 + 添加新属性
4.为新属性输入名称和选定的布尔类型
5. 编辑器->创建 NSManagedObject 子类
6. 确保选中正确的数据模型,单击下一步
7. 确保选中正确的实体,点击下一步
8. 选择保存位置(使用根项目文件夹),选择创建
9. 在对话框中选择“替换”
10. 在生成的头文件中查找变量。变量不存在。
11. 关闭 Xcode6
12. 在 Xcode 5.1.1 中重复步骤 1-10,工作正常
13. 重复此过程两次以验证相同的结果

Thanks for any help!

谢谢你的帮助!

回答by d0n13

Ok, I figured out a workaround but it would appear this is a bug.

好的,我想出了一个解决方法,但看起来这是一个错误。

For all the entities you want to regenerate, remove the existing files from disk try Editor->Create NSManagedObject subclass and select those classes that you have removed from disk.

对于要重新生成的所有实体,从磁盘中删除现有文件,尝试 Editor->Create NSManagedObject subclass,然后选择已从磁盘中删除的那些类。

The files that don't currently exist are regenerated. This fixed the issue for me.

重新生成当前不存在的文件。这为我解决了这个问题。

回答by SpartaqUA

I had the same problem, Xcode didn't generate new properties after I choose "Replace" files.
Just fix it doing next steps:

我遇到了同样的问题,在我选择“替换”文件后,Xcode 没有生成新的属性。
只需执行以下步骤即可修复它:

  • after you change or add new entity in .xcdatamodel select changed entity Class files at "Groups & Files" list
  • select "Delete" - "Move to Trash"(if need copy specific code from your class file)
  • now back to .xcdatamodel and select entity you changed
  • create new NSManagedObject subclass(use standard way)
  • 在 .xcdatamodel 中更改或添加新实体后,在“组和文件”列表中选择已更改的实体类文件
  • 选择“删除”-“移至垃圾箱”(如果需要从类文件中复制特定代码)
  • 现在回到 .xcdatamodel 并选择您更改的实体
  • 创建新的 NSManagedObject 子类(使用标准方式)

回答by Ed Manners

This requires a workaround, but specifically the quickest way I found was:

这需要一种解决方法,但特别是我发现的最快方法是:

  1. Setup your project such that the generated files from the model, are in a folder of their own.

  2. When you need to add/change attributes to your model, multi-select the set of auto-generated files, and hit backspace (and select move them to Trash)

  3. Re-generate your set of NSManagedObject files from the Model, back into the same folder

  1. 设置您的项目,以便从模型生成的文件位于它们自己的文件夹中。

  2. 当您需要向模型添加/更改属性时,多选一组自动生成的文件,然后按退格键(并选择将它们移至垃圾箱)

  3. 从模型重新生成一组 NSManagedObject 文件,回到同一个文件夹

回答by milos

Before deleting your files and/or the data model itself as suggested, check whether you have added the module name in the Xcode's Data Model Inspector under Entity/Class. If you have, removing it will solve the problem of auto generating the entity classes. It will, however, reintroduce the problem you fixed by adding it in the first place (see Swift - CoreData: warning: Unable to load class named)... With this in mind, it appears a better solution to add the @objc(<ClassName>)above the generated class than to add the module name in the inspector.

在按照建议删除文件和/或数据模型本身之前,请检查您是否在 Xcode 的数据模型检查器中的实体/类下添加了模块名称。如果有,删除它可以解决自动生成实体类的问题。但是,它会重新引入您通过首先添加它解决的问题(请参阅Swift - CoreData: 警告:无法加载名为 的类)......考虑到这一点,添加@objc(<ClassName>)上面生成的内容似乎是一个更好的解决方案类而不是在检查器中添加模块名称。

回答by losiu

Old question. But here is my solution: just CLEAN it and BUILD again.

老问题。但这是我的解决方案:只需清理它并再次构建。

Menu: project > clean build folder and build again.

菜单:项目 > 清理构建文件夹并再次构建。

btw, here is the time of Xcode 9.

顺便说一句,这是 Xcode 9 的时间。