Duplicate symbol error when adding NSManagedObject subclass, duplicate link

后端 未结 6 1530
情深已故
情深已故 2020-12-13 13:02

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

相关标签:
6条回答
  • 2020-12-13 13:43

    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.

    0 讨论(0)
  • 2020-12-13 13:50

    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.

    0 讨论(0)
  • 2020-12-13 13:51

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

    0 讨论(0)
  • 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)

    0 讨论(0)
  • 2020-12-13 13:53

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

    0 讨论(0)
  • 2020-12-13 13:55

    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.

    0 讨论(0)
提交回复
热议问题