xcode 添加 NSManagedObject 子类时出现重复符号错误,重复链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40460307/
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
Duplicate symbol error when adding NSManagedObject subclass, duplicate link
提问by KFZ
I was trying to create NSManagedObject subclasses (2 related entities) automatically in Xcode. They are generated like this:
我试图在 Xcode 中自动创建 NSManagedObject 子类(2 个相关实体)。它们是这样生成的:
However, before I do anything further, when I tried to build and run it, a link error occur, as shown:
但是,在我做进一步的事情之前,当我尝试构建和运行它时,出现链接错误,如图所示:
duplicate symbol _OBJC_CLASS_$_Photo in:
/Users/Kefeng/Library/Developer/Xcode/DerivedData/Photomania-aellrakjngugnzcgrleiytvrfvyt/Build/Intermediates/Photomania.build/Debug-iphonesimulator/Photomania.build/Objects-normal/x86_64/Photo+CoreDataClass.o
duplicate symbol _OBJC_METACLASS_$_Photo in:
/Users/Kefeng/Library/Developer/Xcode/DerivedData/Photomania-aellrakjngugnzcgrleiytvrfvyt/Build/Intermediates/Photomania.build/Debug-iphonesimulator/Photomania.build/Objects-normal/x86_64/Photo+CoreDataClass.o
duplicate symbol _OBJC_CLASS_$_Photography in:
/Users/Kefeng/Library/Developer/Xcode/DerivedData/Photomania-aellrakjngugnzcgrleiytvrfvyt/Build/Intermediates/Photomania.build/Debug-iphonesimulator/Photomania.build/Objects-normal/x86_64/Photography+CoreDataClass.o
duplicate symbol _OBJC_METACLASS_$_Photography in:
/Users/Kefeng/Library/Developer/Xcode/DerivedData/Photomania-aellrakjngugnzcgrleiytvrfvyt/Build/Intermediates/Photomania.build/Debug-iphonesimulator/Photomania.build/Objects-normal/x86_64/Photography+CoreDataClass.o
ld: 4 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
duplicate symbol _OBJC_CLASS_$_Photo in:
/Users/Kefeng/Library/Developer/Xcode/DerivedData/Photomania-aellrakjngugnzcgrleiytvrfvyt/Build/Intermediates/Photomania.build/Debug-iphonesimulator/Photomania.build/Objects-normal/x86_64/Photo+CoreDataClass.o
duplicate symbol _OBJC_METACLASS_$_Photo in:
/Users/Kefeng/Library/Developer/Xcode/DerivedData/Photomania-aellrakjngugnzcgrleiytvrfvyt/Build/Intermediates/Photomania.build/Debug-iphonesimulator/Photomania.build/Objects-normal/x86_64/Photo+CoreDataClass.o
duplicate symbol _OBJC_CLASS_$_Photography in:
/Users/Kefeng/Library/Developer/Xcode/DerivedData/Photomania-aellrakjngugnzcgrleiytvrfvyt/Build/Intermediates/Photomania.build/Debug-iphonesimulator/Photomania.build/Objects-normal/x86_64/Photography+CoreDataClass.o
duplicate symbol _OBJC_METACLASS_$_Photography in:
/Users/Kefeng/Library/Developer/Xcode/DerivedData/Photomania-aellrakjngugnzcgrleiytvrfvyt/Build/Intermediates/Photomania.build/Debug-iphonesimulator/Photomania.build/Objects-normal/x86_64/Photography+CoreDataClass.o
ld: 4 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I tried several times by creating new projects and do the same thing. My original intention is to add some custom methods into those to subclasses. But when I add anything into e.g. Photo+CoreData.h/m
, the same error as above showed up.
我通过创建新项目尝试了几次并做同样的事情。我的初衷是将一些自定义方法添加到子类中。但是当我在 eg 中添加任何东西时Photo+CoreData.h/m
,出现了与上面相同的错误。
I found some answers about the "double include" or "save files to the wrong directory", but I didn't do that. Anybody have any idea about this?
我找到了一些关于“双重包含”或“将文件保存到错误目录”的答案,但我没有这样做。有人对此有任何想法吗?
回答by Andrey Seredkin
回答by Mike Critchley
Edit: Thanks to some help from @iPeter, found the following:
编辑:感谢@iPeter 的帮助,发现以下内容:
After doing Editor > Generate NSManagedObject files, if you trash the files BEFORE building, your project should build no problems.
在执行 Editor > Generate NSManagedObject files 之后,如果您在构建之前删除文件,您的项目应该构建没有问题。
Then #import "myManagedObjectName+CoreDataClass.h" (where the MO name is the one in the entity inspector in core data) into any classes where you require those Managed Objects.
然后将“myManagedObjectName+CoreDataClass.h”(其中 MO 名称是核心数据中实体检查器中的名称)#import 到您需要这些托管对象的任何类中。
In other words, you don't require any of the actual ManagedObject files in your folder. Xcode keeps the generated ones in your Derived Data folder.
换句话说,您的文件夹中不需要任何实际的 ManagedObject 文件。Xcode 将生成的数据保存在您的派生数据文件夹中。
If for some reason you need those files to remain in your file directory, the following workaround will work. Go to your Target and delete the CoreDataClass sources in your Compile Sources.
如果出于某种原因您需要将这些文件保留在您的文件目录中,则以下解决方法将起作用。转到您的目标并删除编译源中的 CoreDataClass 源。
Leaving you with this:
留给你这个:
- Most of the new attributes / relationships I added after the initial generation of ManagedObject subclasses were available as properties after a build. In one case where I renamed an existing relationship, I had to do Editor > Generate NSManagedObject Subclasses again, then I trashed the new files in my folder, built, and everything worked OK.
- 我在初始生成 ManagedObject 子类之后添加的大多数新属性/关系在构建后都可用作属性。在我重命名现有关系的一种情况下,我必须再次执行编辑器 > 生成 NSManagedObject 子类,然后我将文件夹中的新文件丢弃,构建,一切正常。
Just wrote a blog post that includes this infofor anybody interested.
刚刚写了一篇博客文章,其中包含任何感兴趣的人的信息。
回答by Earl0Grey
You should delete all these entities, change "Codegen" settings to "Manual/None" for them in Data Model Inspector, and generate entities again. It works good! You don't need to remove +CoreDataClass.h files from Compile Source. You don't need to mark entities as abstract. You don't need to generate classes by yourself. You should change only "Codegen" settings and regenerate entities automatically.
您应该删除所有这些实体,在 Data Model Inspector 中将它们的“Codegen”设置更改为“Manual/None”,然后再次生成实体。它运作良好!您不需要从编译源中删除 +CoreDataClass.h 文件。您不需要将实体标记为抽象。您不需要自己生成类。您应该只更改“Codegen”设置并自动重新生成实体。
回答by Chris
late post ... but for me was simply a copy paste of an entity, Xcode does not seem to change the original class name associated with the entity (observed on Xcode 9.0.1)
迟到的帖子……但对我来说只是一个实体的复制粘贴,Xcode 似乎没有改变与实体关联的原始类名(在 Xcode 9.0.1 上观察到)
回答by FrogInNightCap
You need to remove the CoreData related sub class generated by yourself, and you can keep the #import line with those classes, now all those files will be generated automatically when build the project.
您需要删除自己生成的CoreData相关子类,您可以保留这些类的#import行,现在所有这些文件都会在构建项目时自动生成。
回答by Antzi
I recently tried the above answer and it failed. Somehow, Xcode was still generating the files.
我最近尝试了上述答案,但失败了。不知何故,Xcode 仍在生成文件。
I solved it using the following method:
我使用以下方法解决了它:
- Mark all entities as Abstract
- Build
- Remove Abstract flag
- Build
- Success !
- 将所有实体标记为抽象
- 建造
- 删除抽象标志
- 建造
- 成功 !
This sounds like a bug in Xcode...
这听起来像是 Xcode 中的一个错误...