Technical Q&A QA1952

Creating Core Data Managed Object Subclasses with Xcode

Q:  How do I create Core Data managed object subclasses for my entities with Xcode?

A: Follow these steps to create Core Data managed object subclasses with Xcode:

  1. Select your Core Data model in the project navigator.

    Xcode displays all your entities in the outline view of the editor area.

  2. Select an entity in the outline view, and verify that the Codegen pop-up menu is set to Manual/None in the Data Model inspector as shown in Figure 1. Repeat this step for all entities you want to create Core Data managed object subclasses.

Figure 1  Setting the Codegen pop-up menu to Manual/None for the Person entity. The numbers in this figure correspond to the steps above.
  1. Choose Editor > Create NSManagedObject Subclass… as shown in Figure 2.

Figure 2  Select Create NSManagedObject Subclass... to start generating a Core Data managed object subclass.
  1. In the sheet that appears, select the data model that contain your entities, and click Next.

Figure 3  Select your data model from the list.
  1. In the sheet that appears, select the entities whose Core Data managed object subclasses you want to create, and click Next.

Figure 4  Select your entities from the list.
  1. In the sheet that appears, select a location to save your files, and click Create.

Figure 5  Select a location for your files.

Xcode creates and saves files named ClassName+CoreDataClass and ClassName+CoreDataProperties for each of your selected entity in the selected location where ClassName is the name of your entity's NSManagedObject subclass. ClassName+CoreDataClass implements the NSManagedObject subclass as shown in Figure 6.

Figure 6  Viewing PersonMO+CoreDataClass, a Core Data managed object subclass.

ClassName+CoreDataProperties implements a ClassName+CoreDataClass extension (for Swift apps) or category (for Objective-C apps) as shown in Figure 7.

Figure 7  Viewing PersonMO+CoreDataProperties, a PersonMO+CoreDataClass extension.


Document Revision History


DateNotes
2017-03-09

New document that describes how to create Core Data managed object subclasses with Xcode.