问题描述
我只是用 Core Data 创建了一个演示项目.
我在我的数据模型中创建了一个实体 Userinfo
.现在我创建了这个实体的 NSManagedObject
子类.
Xcode 自动生成了这 4 个类.
现在当我构建项目时它会抛出这个错误:
我已尽我所知消除重复错误,但没有任何帮助.
我认为这是一个 Xcode 错误.请帮忙.
您正在生成 Xcode 已经为您生成的文件,因此会得到重复的声明.可以在
- 清理项目
- 清理 DerivedData 文件夹(可选.用于保存)
- 构建
注意:
切勿将自动生成的文件添加到您的项目中.即使您在项目中看不到生成的文件,Xcode 也有对它的引用,因此您可以编写扩展等.例如:
extension MyEntity {func doSomething() {//}}
此外,您可以在 Xcode 中命令+单击生成的文件.
2) 手动触发子类生成(一种相当偏执但经过验证的方法,忽略了新的 Xcode 功能)
- 从项目中删除所有生成的 NSManagedObject 子类(如果存在).
- 在您的
.xcdatamodel
中将所有实体的Codegen
设置为Manual/None
- 清理项目
- 清理派生数据文件夹
- 重启Xcode
- 手动生成
NSManagedObject
子类(在编辑器"菜单中) - 确保将这些文件添加到您的项目中
- 构建
I just simply created a demo project with Core Data.
I created an entity Userinfo
in my data model. Now I created a NSManagedObject
subclass of this entity.
Xcode autogenerated these 4 classes.
Now when I build the project it throws this error:
I have done everything I know to remove the error of duplicacy but nothing helped.
I think its a Xcode bug. Please help.
You are generating files which have already been generated for you by Xcode and thus get duplicate declarations. Details about this feature (new in Xcode 8) can be found in this WWDC video.
Two possible fixes:
1) Use the Xcode generated ManagedObject
subclasses (the recommended, modern approach)
- Delete all generated NSManagedObject subclasses from your project, if exists.
- Set
Codegen
toClass Definition
in your.xcdatamodel
for all entities - Make sure
Module
is empty ("Global Namespace" in light gray) (workaround an Apple bug, see this answer)
- Clean project
- Clean DerivedData folder (Optional. To be on the save side)
- build
Note:
Never add the automatically generated files to your project. Even you do not see the generated files in your project, Xcode has a reference to it, so you are able to write extensions and such. For instance:
extension MyEntity {
func doSomething() {
//
}
}
Also, you can command+click to the generated file within Xcode.
2) Trigger subclass generation manually (a rather paranoid but bullet-prove approach, ignoring the new Xcode features)
- Delete all generated NSManagedObject subclasses from your project, if exists.
- Set
Codegen
toManual/None
in your.xcdatamodel
for all entities - Clean project
- Clean DerivedData folder
- Restart Xcode
- Manually generate
NSManagedObject
subclasses (in "Editor" menu) - Make sure those files are added to your project
- build
这篇关于NSManagedObject 子类中的重复符号错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!