16

I have created a brand new core data model for my project and I want to create some NSManagedObject Subclasses for it. After generating subclasses XCode throws some errors that reference a path to the /DerivedData folder. See my steps below and image of the error:

enter image description here enter image description here enter image description here

I have already tried clean building, clearing the DerivedData folder, all that hoo-haa. I've also tried creating the generated files manually but I still get the same errors. Has anybody managed to get around this?

rednaxela
  • 661
  • 2
  • 9
  • 21
  • May I know why you want that manual process as CodeData and XCode will manage it automatically by creating subclasses in Derived Data ? – CodeChanger Feb 13 '19 at 08:57
  • Possible duplicate of [Swift CoreData NSManagedObject subclass](https://stackoverflow.com/questions/40390820/swift-coredata-nsmanagedobject-subclass) – Swift Dev Journal Feb 13 '19 at 20:14
  • You don't need to create the NSManagedObject subclasses manually unless if you want any custom changes. The Xcode now generates these for you. – Sachin Vas Feb 14 '19 at 05:33
  • I meet the same issue when add CoreData to my exist project. And a really weird thing is even I choose the CoreData folder for those generated subclass files, Xcode still creates them outside of the top level folder. I need to manually drag them to my CoreData folder for each time. – Zhou Haibo Sep 14 '21 at 11:20

3 Answers3

50

Here is a solution of your issue related to coredata

As per your first screenshot, right hand side you can see field named Codegen under Class, open that dropdown and select Manual/None and generate files again from Editor - > Create NSManageObject Subclass, issue will be solve.

See this image

Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
  • Yes, Editor - > Create NSManageObject Subclass will create your subclasses, but it won't update them when you change, add or delete an attribute. Codegen's Category/Extension will take care of that drudgery for you. – Elise van Looij Nov 22 '20 at 16:24
  • If it does not work, remove the existing files generated then remove Build Folder and Derived Data. Restart Xcode and you can now generate it again following this answer. – Kevin Machado May 23 '22 at 05:14
7

I ran into the same problem after some heavy editing of my DataModel. All my entities use Codegen Category/Extension and subclasses, and suddenly I got this cryptic error:

error: Multiple commands produce '/Library/Developer/Xcode/DerivedData/MyApp-futvjnnhiceyibabpbzhxltebhoq/Build/Intermediates.noindex/MyApp.build/Debug/MyApp.build/DerivedSources/CoreDataGenerated/MyApp/MyEntity+CoreDataProperties.m':

  1. Target 'MyApp' (project 'MyApp'): DataModelCodegen /Users/elisevanlooij/Documents/Project MyApp/MyApp/MyApp/MyApp.xcdatamodeld
  2. Target 'MyApp' (project 'MyApp'): DataModelCodegen /Users/elisevanlooij/Documents/Project MyApp/MyApp/MyApp/MyApp.xcdatamodeld

After two days I found the problem: the Entity Name and the Entity Class Name no longer matched in the DataModel, forcing Codegen to generate two extensions for one entity.Once I fixed that, threw away the app's Intermediates folder in ~/Library/Developer/Xcode/DerivedData and did a Product > Clean Build Folder, everything compiled beautifully again.

The error message is technically correct, but massively unhelpful. The DataModel interface should flag this as soon as it happens, but one can only dream of a day when Apple pays proper attention to Core Data.

Elise van Looij
  • 4,162
  • 3
  • 29
  • 52
  • For my case, I use the same name with one entity name and one of Model name, then Xcode cannot deal with it. After change one of them's name, issue is fixed. – Zhou Haibo Jun 21 '21 at 07:16
  • Thank you! While the accepted solution solved my problem, this solution is the right one and the better one. Great answer, thanks! – Zoli Feb 08 '22 at 17:59
3

enter image description here

The Codegen has three options:

  • Manual/None. You can use the Editor > "Create NSManagedObject Subclass" menu to generate both the +CoreDataClass and +CoreDataProperties files.
  • Class Definition. The default option. Xcode generates both of the two files automatically in the build process. You should not add these files in your project.
  • Category/Extension. Xcode automatically generates the extension +CoreDataProperties file. So if you use the "Create NSManagedObject Subclass" menu to generate the two files, you need to remove the +CoreDataProperties file manually, leaving only the +CoreDataClass file in the project.
Cosyn
  • 4,404
  • 1
  • 33
  • 26