Duplicate symbol error when adding NSManagedObject

2020-02-07 20:00发布

I was trying to create NSManagedObject subclasses (2 related entities) automatically in Xcode. They are generated like this:

enter image description here

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)

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.

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?

6条回答
Explosion°爆炸
2楼-- · 2020-02-07 20:18

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.

查看更多
姐就是有狂的资本
3楼-- · 2020-02-07 20:21

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. enter image description here

查看更多
迷人小祖宗
4楼-- · 2020-02-07 20:23

Edit: Thanks to some help from @iPeter, found the following:

After doing Editor > Generate NSManagedObject files, if you trash the files BEFORE building, your project should build no problems.

Trash these 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.

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.

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.

Before

Leaving you with this:

After

  • 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.

Just wrote a blog post that includes this info for anybody interested.

查看更多
老娘就宠你
5楼-- · 2020-02-07 20:25

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)

查看更多
孤傲高冷的网名
6楼-- · 2020-02-07 20:27

If you do not generate managed object subclass automatically, then don't forget to check "Codegen" settings for an Entity in Data Model Inspector:

enter image description here

查看更多
来,给爷笑一个
7楼-- · 2020-02-07 20:28

I recently tried the above answer and it failed. Somehow, Xcode was still generating the files.

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...

查看更多
登录 后发表回答