Core Data Code Generation

Starting with Xcode 8, you can let Xcode generate the code for your Core Data entities. Access an entity’s code generation options by selecting the entity and opening the data model inspector.

Xcode Core Data code gen menu

The Codegen menu is where you specify the code generation for the entity. There are three code generation options.

  • Choosing Manual/None tells Xcode not to generate code for the entity. You must create the class for the entity by creating a new file or by choosing Editor > Create NSManagedObject Subclass.
  • Choosing Class Definition tells Xcode to create the entity’s class files when you build the project.
  • Choosing Category/Extension tells Xcode to create a Swift class extension for the entity when you build the project. The class extension has accessors for the entity’s attributes.

Where are the Source Code Files?

If you choose Class Definition or Category/Extension from the Codegen menu, you’ll notice there are no source code files for the entities in the project navigator. Where are the files?

The source code files are in the project’s derived data location. You’re not meant to access and edit the source code files Xcode creates for your Core Data entities. The point of Xcode creating the files is to automatically update the files when you make changes to the data model.

What do you do if you want to add methods to your Core Data entities? Create Swift class extension files for your entities and add your methods there.

Duplicate Classes

When you create a new Xcode project that uses Core Data, the code generation is initially set to Class Definition. If you manually add class files for your entities, your project won’t build because you have two versions of each class, the one you manually created and the one Xcode creates. This issue can be tough to find because you can’t see the Xcode-created class files in the project navigator.

There are two ways to deal with the duplicate class problem. The first way is to remove the class files you added to the project and let Xcode create the classes for you. The second way is to switch to manual code generation by choosing Manual/None from the Codegen menu.

Which Code Generation Option Should You Choose?

Choose manual code generation if you’re using version control in your project. By writing the entity’s code yourself, you can track the changes in your version control system and go back if you make a mistake.

Choose the Class Definition code generation option if you don’t want to worry about keeping the data model and code in sync. When you make changes to the data model, you don’t have to worry about updating the code if you choose Class Definition. Xcode updates the code for you.

Choose the Category/Extension code generation option if you need more control over the class hierarchy. If you choose the Class Definition code generation option or choose Editor > Create NSManagedObject Subclass to generate your classes manually, Xcode assumes the entity and class hierarchies are the same. If you want a different class hierarchy for your data model, choose the Category/Extension code generation option.

Get the Swift Dev Journal Newsletter

Subscribe and get exclusive articles, a free guide on moving from tutorials to making your first app, notices of sales on books, and anything I decide to add in the future.

    We won't send you spam. Unsubscribe at any time.