Duplicate Symbol Error in NSManagedObject Subclass

2020-01-29 18:12发布

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.

1条回答
Viruses.
2楼-- · 2020-01-29 18:36

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)

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
查看更多
登录 后发表回答