Core Data Codegen Explained!

No more head-scratching

Lee Kah Seng
6 min readFeb 15, 2018

--

Starting from Xcode8, Apple has added a new Codegen setting in the Xcode data model editor to help developer manage and maintain their NSManagedObject subclasses. The Codegen setting consist of 3 possible configurations:

  • Manual/None
  • Class Definition
  • Category/Extension

These 3 configurations might look a bit confusing at first, and I found out that it is actually quite difficult to find a good article that explain the differences between each configuration and which one to use.

In this article I will go through each configuration and analyse the differences between each of them and I will also give some suggestion on which one you should use and how you should use it for your Core Data project. Without further ado let’s get started!

Manual/None

This is the default behaviour prior to Xcode8 where developer have to manually create and maintain the changes of the NSManagedObject subclasses.

You can try to create a sample core data project, add a new entity call TestEntity and generate NSManagedObject subclass following the step in this link.

If you follow the step correctly, Xcode will generate 2 files:

TestEntity+CoreDataClass.swift
TestEntity+CoreDataProperties.swift
TestEntity created for sample core data project.

After these 2 files been generated, you should be able to use TestEntity in your project. Try copy the following line of code into your app delegate, your project should compile without any error.

let _ = TestEntity(context: persistentContainer.viewContext)

Class Definition

This configuration is the default Codegen configuration when you create an entity in the data model editor. With this configuration, Xcode will automatically generate the required NSManagedObject subclass as part of the project’s derived data.

To see this in action, open the sample core data project that you created previously and delete both…

--

--

Lee Kah Seng

Support me by becoming a Medium member: https://leekahseng.medium.com/membership ⦿ 🇲🇾 Creator of https://swiftsenpai.com ⦿ iOS developer since 2011