Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate Symbol Error in NSManagedObject Subclass

I just simply created a demo project with Core Data.

I created an entity Userinfo in my data model. Now I created a NSManagedObject subclass of this entity.

Xcode autogenerated these 4 classes.

enter image description here

Now when I build the project it throws this error:

enter image description here

I have done everything I know to remove the error of duplicacy but nothing helped.

I think its a Xcode bug. Please help.

like image 767
iPeter Avatar asked Nov 29 '16 13:11

iPeter


1 Answers

You are generating files which have already been generated for you by Xcode and thus get duplicate declarations. Details about this feature (new in Xcode 8) can be found in this WWDC video.

Two possible fixes:

1) Use the Xcode generated ManagedObject subclasses (the recommended, modern approach)

  • Delete all generated NSManagedObject subclasses from your project, if exists.
  • Set Codegento Class Definition in your .xcdatamodel for all entities
  • Make sure Module is empty ("Global Namespace" in light gray) (workaround an Apple bug, see this answer)

  • Clean project
  • Clean DerivedData folder (Optional. To be on the save side)
  • build

Note:

Never add the automatically generated files to your project. Even you do not see the generated files in your project, Xcode has a reference to it, so you are able to write extensions and such. For instance:

extension MyEntity {
    func doSomething() {
        //
    }
}

Also, you can command+click to the generated file within Xcode.

2) Trigger subclass generation manually (a rather paranoid but bullet-prove approach, ignoring the new Xcode features)

  • Delete all generated NSManagedObject subclasses from your project, if exists.
  • Set Codegento Manual/None in your .xcdatamodel for all entities
  • Clean project
  • Clean DerivedData folder
  • Restart Xcode
  • Manually generate NSManagedObject subclasses (in "Editor" menu)
  • Make sure those files are added to your project
  • build
like image 168
shallowThought Avatar answered Nov 16 '22 18:11

shallowThought