Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate symbol error when adding NSManagedObject subclass, duplicate link

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?

like image 488
KFZ Avatar asked Nov 07 '16 07:11

KFZ


People also ask

What is duplicate symbol error?

Solution. Duplicate symbols occur when you have both added an implementation file (. cpp) to your project and #included it. This way, the implementation file (. cpp) gets compiled twice: once as a module in your project (as it is added to your project) and subsequently as a piece of #included code.

How do I create a subclass in NSManagedObject?

From the Xcode menu bar, choose Editor > Create NSManagedObject Subclass. Select your data model, then the appropriate entity, and choose where to save the files. Xcode places both class and properties files into your project.


2 Answers

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

like image 86
Andrey Seredkin Avatar answered Sep 20 '22 04:09

Andrey Seredkin


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.

like image 30
Mike Critchley Avatar answered Sep 22 '22 04:09

Mike Critchley