Duplicate symbol error when adding NSManagedObject subclass, duplicate link
Asked Answered
T

6

36

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?

Toni answered 7/11, 2016 at 7:57 Comment(2)
I have the exact same problem. No solution yet... but you're not alone...Dignify
If you are using Xcode 8 then this is the answer: https://mcmap.net/q/162320/-xcode-beta-8-can-39-t-create-core-dataDelainedelainey
W
60

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

Warila answered 24/2, 2017 at 1:10 Comment(1)
This is the correct answer and should be accepted. The answer with the file trashing after generation just makes no sense to me.Chen
G
43

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.

Gentianella answered 13/12, 2016 at 19:24 Comment(7)
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?Spratt
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).Gentianella
So what if we want to extend the managed object classes and add some functionalities there?Lardy
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. :)Gentianella
@PrajeetShrestha Just add a category to the object you wish to extend.Screwy
@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.Screwy
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.Chen
I
33

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

Icj answered 2/4, 2017 at 9:46 Comment(0)
A
1

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)

Amblygonite answered 26/10, 2017 at 8:7 Comment(0)
C
0

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.

Cuboid answered 10/11, 2016 at 16:24 Comment(0)
A
-1

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

Arnold answered 7/3, 2017 at 6:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.