Duplicate symbol error when adding NSManagedObject subclass, duplicate link - ios

I was trying to create NSManagedObject subclasses (2 related entities) automatically in Xcode. They are generated like this:
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?

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

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.
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.
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.
Just wrote a blog post that includes this info for anybody interested.

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.

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)

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.

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

Related

xcode8 header not found using NSManagedObject subclass in CoreData

Thanks for having a look :)
I am using xcode 8.2.1 and the language is obj-c.
I simply created two entities(Photo and Photographer) and after I did editor - create NSManagedObject Subclass there went error in the files generated.
One is in Photo+CoreDataClass.m saying "Photographer.h file not found".
Another one is in Photographer+CoreDataClass.m at the line saying "Photo.h file not found".
All the files that were generated were:
Photographer+CoreDataClass.h/m,
Photo+CoreDataClass.h/m
Photographer+CoreDataProperties.h/m
Photo+CoreDataProperties.h/m
The tools version minimum is Xcode 7.3 and codegen is None/Manual
Could anyone tell me how to fix this?
Ps, the entity's relationship is that one Photographer to-many Photo, and Photo to-one Photographer
Just rename your files
Photographer+CoreDataClass.h/m,
to
Photographer.h/m
and
Photo+CoreDataClass.h/m
to
Photo.h/m
then copy the properties from the other two classes into the renamed ones then remove them.
I got rid of this error by setting Module to empty for the Class section after you select .xcmodeld file

Xcode not generating correct coredata model files

Im generating my core data model after changing my models files using Editor -> Create NSManagedObj...
I changed an entity relationship to a different entity (if this matters)
Iv cleaned my project and build folder.
But the generated files don't seem to be correct, they include files such as 'Entity.h'
Which doesn't exist... As the only class files i see are 'Entity+CoreDataClass.h'
So i change the name manually to the all above errors and then i get a linker error:
duplicate symbol _OBJC_CLASS_$_Userinfo in:
/Users/Me/Library/Developer/Xcode/DerivedData/test-fxdxcviituhcmoegqygwirddhuir/Build/Intermediates/test.build/Debug-iphonesimulator/test.build/Objects-normal/x86_64/Userinfo+CoreDataClass.o
duplicate symbol _OBJC_METACLASS_$_Userinfo in:
/Users/Me/Library/Developer/Xcode/DerivedData/test-fxdxcviituhcmoegqygwirddhuir/Build/Intermediates/test/Debug-iphonesimulator/test/Objects-normal/x86_64/Userinfo+CoreDataClass.o
ld: 2 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Originally i created the project using an older version of Xcode, where the entity files such as 'Entity.h" were generated... Is this some sort of bug with Xcode after updating?
My entities are using the 'manual/none' codegen option. But i notice after building that the entity with the above linker error automatically changes to 'Class Definition'?
EDIT:
Ok so setting the codegen to class definition to all entities stops the wrong file import errors. (but then still revert back to 'manual/none' afterwards. Except for my newly created entity.
Where there is the error in the derriveddata for the +CoreDataClass.m file... where it does not include the +CoreDataClass in the import name.
My +CoreDataClass.m file within my project does not include this error and is correct with the +CoreDataClass.
My project generated file:
#import "Userinfo+CoreDataClass.h"
#import "User+CoreDataClass.h"
#implementation Userinfo
#end
Where the error exists: (in derived sources)
#import "Userinfo+CoreDataClass.h"
#import "User.h"
#implementation Userinfo
#end
Try to click on project. Next click the "Show version editor" (the top right button on the picture) button and try to find duplicated references and remove it.
Should solve the problem
I think I found the answer, in your xcddatamodel file after choosing your entity. On the right panel, you will see a "Code Generation" DropDown. It might default to Swift, change it to Objective c and build the project. Your file will be generated.

"In the uniqueness constraints attribute for entity, comma is not valid property" in Coredata Xcode 8

I created a database with Core Data on iOS. I initially set up a unique constraint in my Conversation entity. However, after removing it, I was getting an error: "In the uniqueness constraints attribute for entity Conversation, comma is not a valid property".
I looked at every field in my DataModel.xcdatamodeld but could not find any solution.
If you have set any constrains (using Attribute name) previously for your Entity and later if you would have deleted/renamed that particular attribute, then it will throw such error. You need to rename or delete that constraint property by double clicking it with respect to your data model business flow.
Refer screenshot for reference.
I had the same issue with Xcode 8.1. I did not want to delete all the many attributes painfully set up on all 6 of my entities, and found another solution. Let's assume your model file is called "foobar.xcdatamodeld".
Edit the XML directly in the model. For this, you need to get to the file 'contents' inside the model file. The path to it is: foobar.xcdatamodeld/foobar.xcdatamodel/contents. To get to it in Finder, control-click on the "xcdatamodeld" package to "Show Package Contents", then again on the "xcdatamodel" package. You can then drag the file 'Contents' into your text editor of choice. You will see the constraints appear as follows in the XML:
<uniquenessConstraints>
<uniquenessConstraint>
<constraint value="property_name"/>
</uniquenessConstraint>
</uniquenessConstraints>
You can edit "property_name" to match the new value, or remove the constraints altogether.
Once edited, try to clean and recompile. In my case, the error was still there, so I had to do a little "renaming dance" with Xcode. I deleted the model from the Xcode project, then renamed the model file to a different name (including the "xcdatamodel" file inside the "xcdatamodeld" file), and added it back to Xcode. I repeated that again with the old name again so I was back to the initial name.
Its looks like a bug in Xcode 8. I have deleted all the attributes from the entity, cleaned the project and then added the attributes again. After that, the error was gone. Maybe this will help someone.
I inspected the XML file and noted that the "comma,separated,properties" was in the XML. I used Product->Clean (command+shift+K) to clean the project. Cleaning in of itself didn't help. I then tried File->Save (command+S).
File->save updated the XML and cleared up the situation for me.

Objective C : linker command failed with exit code 1

This is the error I'm getting when I try to build my app.
ld: 10 duplicate symbols for architecture arm64
From the error, I would guess that:
you have two files, VaultSelectionViewController.m and SetLockViewController.m (the first being originally a copy of the second, probably)
they both contain #implementation SetLockViewController, and identical properties and/or methods and/or instance variables.
Solution: fix the class name in the #implementation to match the file name.
If that's not the issue, please provide the relevant parts of both files.
Your project twice referring to file SetLockViewController.m.
My suggestion is take a copy of class out side of your project.
[1] Then right click on SetLockViewController.m file and delete > move to trash.
[2] Go to project files directory in finder. Then delete the file SetLockViewController.m (if you find any).
[3] Clean the build and run.
Follow same method if any other duplicate files/linker errors present.
Make sure your compile sources does not have dup files. (Bad merge can cause this issue.) Remove them and add them again can fix this build error.
Make sure you does not implement one function twice. (Copy codes and forget to rename the #implement XXX? Or copy and paste one function twice?) Just search the function define in your project and remove the dup one.
Please check all the variables name in every classes of your project and make sure that all they have unique name including outside the class

Duplicate Symbol Error in Objective-C build?

I got this error when I press build+debug:
ld: duplicate symbol .objc_class_name_BlogTableItemCell in /Users/fabian/Development/Workspaces/iphone_experiments/xcode_build_output/MausLog.build/Debug-iphonesimulator/MausLog.build/Objects-normal/i386/BlogTableItemCell-3733583914888A7B.o and /Users/fabian/Development/Workspaces/iphone_experiments/xcode_build_output/MausLog.build/Debug-iphonesimulator/MausLog.build/Objects-normal/i386/BlogTableItemCell-3733583914888A7B.o
collect2: ld returned 1 exit status
Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/gcc-4.2 failed with exit code 1
You could also get this error if you mistakenly let Xcode's auto-complete for #import statements specify the '.m" file for the 'duplicate' class instead of the '.h'.
It seems that you are compiling the same BlogTableItemCell class two times in different places of your code. This may happen in the following cases.
You have put the same class
implementation into two different
files;
You actually have just one
implementation of this class, however
you are also linking in your project a framework
or library containing a class whose
name is exactly the same of yours.
Try finding in the whole project your class and make sure only one copy is available within your project.
For me, changing 'No Common Blocks' from Yes to No ( under Targets->Build Settings->Apple LLVM - Code Generation )
I had a similar problem due to poor defining of consts.
I had defined a const in my header:
int const kCropLocationTop = 1;
This was presumably imported multiple times. To fix i changed the header def as follows:
extern int const kCropLocationTop;
and moved the assigning of the const to the .m file:
int const kCropLocationTop = 1;
Hope that helps anyone who's as ignorant of simple objective c concepts as I am!
iPhone: Duplicate Symbol Error? by user576924
answered it correctly for me. However to find the offending gremlin this ZSH snippet.
grep "import.*\.m" **/*.[hm]
Will immediately tell you where your error is.
By mistake the source file was included twice in the Project -> Build Phase -> Compile Sources. Removing one of them solved the problem.
The most common reason for this error is importing an xyz.m file instead of the xyz.h file.
Check if your imports contain something like
#import "----.m"
Just to add; Using Xcode to generate subclassed managed objects (Core Data) can sometimes duplicate the generated files. For me the fix was to delete the generated files and re-generate them.
I just ran into this problem myself. For the list, here's another possibility:
Duplicated linking line in the project file.
I caused this merging conflicts on a SVN update, when I accidentally duplicated a line.
It happened to me, too. In my case, one (just one) of my core data automatically generated classes was inserted twice. I spotted the duplication by looking at Build Phases...Compile Sources. Simply deleting one of the occurrences solved the problem.
Adding another possible cause to the list... you may have mistakingly created multiple constants in the implementation file, but outside of the implementation, with the same name.
In HeaderFileOne.m
NSString * const kCoolConstant = #"cool";
In HeaderFileTwo.m
NSString * const kCoolConstant = #"cool";
So changing one of those constant names would fix the compile error.
This may help someone
I got this error because I duplicate a ViewController and then renamed it. So when I compile I got this error. The reason was in both of the view controllers there was a "float" variable with same name i.e "float padding=10.0" which I had defined on class level.
Renaming the name of the above mentioned variable in One of the view controllers solved my problem.
I also faced to this problem. My solution was rename one of global variable, which has the same name as one in other class. Hope this helps
The same thing happened to me while I was playing with localizable xib files, accidentally I have created two implementation files and appereantly that caused the problem in my case. After deleting / recreating the implementation file without doing the same mistake, the error was fixed.
One of our developers left the "libSoomla*" project files in there twice. I removed the duplicate soomla files, re-built, and that fixed it!
Hope it helps.
In may case, I followed some instructions to build a newer version of Subversion which directed me to create this symbolic link:
ln -s /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/ /Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.10.xctoolchain
Now I'm really a Windows guy so it wasn't immediately obvious to me - but removing the link fixed it for me (after restarting XCode):
rm /Applications/Xcode.app/Contents/Developer/Toolchains/OSX10.10.xctoolchain
Phew.
(The actual error I got was the one described here:
build error duplicate symbols arclite.o)
Make sure that you didn't import .m File . For me this happen I added #import "SchoolCommuterHome.m" instead of #import "SchoolCommuterHome.h"

Resources