Duplicate symbol error when adding NSManagedObject subclass, duplicate link

12,156

Solution 1

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

Solution 2

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.

Solution 3

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

Solution 4

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)

Share:
12,156
KFZ
Author by

KFZ

Updated on July 08, 2022

Comments

  • KFZ
    KFZ almost 2 years

    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?

  • surfrider
    surfrider over 7 years
    I have tried this method and it worked for me. Thanks. But is it 'proper' way to generate CoreData classes? Can it lead to some troubles in future? And why XCode can't do it itself?
  • Mike Critchley
    Mike Critchley over 7 years
    I actually did ask Xcode to generate them via the Editor menu. Without that step, Xcode was not adding files to my Derived Data folder. After doing the deletes above and building once, I find now that I can trash all the files in my project folder that Xcode generated and just use import statements alone (see edits to my answer above).
  • Prajeet Shrestha
    Prajeet Shrestha over 7 years
    So what if we want to extend the managed object classes and add some functionalities there?
  • Mike Critchley
    Mike Critchley over 7 years
    Hi Prajeet! A managed object is a data object. Its only purpose should be to know what data and relationships it manages. So I wouldn't add methods to that object. Their one purpose is to hold data. If you're getting them to do something with the data, you're muddying the waters. Best to keep implementation OUT of your Managed Objects. :)
  • EricLeaf
    EricLeaf about 7 years
    @PrajeetShrestha Just add a category to the object you wish to extend.
  • EricLeaf
    EricLeaf about 7 years
    @MikeCritchley You're missing a valuable distinction. If I want to use data from a particular object, saying a sorted list (which is a set in CD) adding this as a category of the object is perfect. Additionally this allows one to perhaps efficiently store this, or take advantage of transient data.
  • AirXygène
    AirXygène about 6 years
    This is the correct answer and should be accepted. The answer with the file trashing after generation just makes no sense to me.
  • AirXygène
    AirXygène about 6 years
    Sorry, but to me this just doesn't make sense. You have 2 choices. Either you let XCode generate the source file transparently in the derived data, or you generate them via the command menu and then you use them. Mixing those two ways by generating via the menu, removing the files from the project and using the derived data is just wrong. If you generate them via the menu it is because you want them to be used/compiled, most probably you want to do something with it. In that case, just set the CodeGen menu to "Manual/None" as explained by Andrey Seredkin in the other answer.