63

I am working with CoreData, on an entity called "RoleName".

The problem is: I click on "Create NSManagedObject subclass" from within my model, and so it automatically creates the classes for my entity.

However, on the declaration of the class, I get this error:

Invalid redeclaration of "RoleName"

even though I don't have any other class with the same name.

TheNeil
  • 3,321
  • 2
  • 27
  • 52
dpstart
  • 1,018
  • 1
  • 10
  • 25

2 Answers2

174

This is because Xcode handles all that by itself. I felt it like a bit of trouble as the auto generated classes don't have all my properties.

So follow these steps to get this as it used to be:

  • Delete what ever classes you already made for core data.
  • Set class.Module as Current Product module enter image description here

  • Set Class.codegen as Manual/None

enter image description here

  • Now select your entity and create NSmanagedobject subclass enter image description here

  • You are all set

Deepukjayan
  • 3,525
  • 1
  • 20
  • 31
18

From Apple : Whats new in Core Data

Xcode automatic subclass generation

Xcode now supports automatic generation of NSManagedObject subclasses in the modeling tool. In the entity inspector:

Manual/None is the default, and previous behavior; in this case you should implement your own subclass or use NSManagedObject. Category/Extension generates a class extension in a file named like ClassName+CoreDataGeneratedProperties. You need to declare/implement the main class (if in Obj-C, via a header the extension can import named ClassName.h). Class Definition generates subclass files named like ClassName+CoreDataClass as well as the files generated for Category/Extension.

The generated files are placed in DerivedData and rebuilt on the first build after the model is saved. They are also indexed by Xcode, so command-clicking on references and fast-opening by filename works.

You don't need to manually create subclasses for NSManagedObjects.

I would suggest that you delete the files that you created with NSManagedObjects (Move them to Trash) and go to every entity in the DataModel Inspector under Codegen select : Manual / None and create than the Subclasses.

FBC
  • 1,047
  • 8
  • 18
  • I am sorry my question wasn't clear about this. Anyway, I generated the subclasses automatically – dpstart Nov 04 '16 at 19:06
  • So if I understand this correctly, When configured with Module "Global namespace" (the default) & Class Definition, or Category/Extension. Xcode automagically creates a class definition for the entity, which it stores in derived data and is not presented to the user as a "normal" .swift file in the project. By running "Create NSManagedObject Subclass..." Without specifying a different module, we end up creating a second class definition in the global namespace, hence the "Invalid redeclaration" error. Do I have that correct? – TMin Jan 31 '23 at 20:01