Implement NSManagedObject Subclass in Swift

Updated on July 8, 2016 – Xcode 7

My goal with this blog entry is to help get you set up to create NSManagedObject subclasses in Swift for the Entities in your Core Data model.

Example

Let’s look at a fabricated example: Say that you’ve got a Core Data project and you’re creating Entities.  For my simple project, I’ll create an Entity called “MyEntity” with an attribute called “myAttribute”.

After you create an NSManagedObject subclass for the Entity and come back to the data model screen to specify the “Class” in the inspector area, you must prefix the name of the class with “YourProjectName.” (don’t forget the dot).  Forgetting to do this will lead to run-time errors when you start interacting with instances of your NSManagedObject subclass.

Apple specifies this in their documentation page, but it was a subtle mention at the end of the document and I just happened upon it as I was troubleshooting this:

Swift classes are namespaced—they’re scoped to the module (typically, the project) they are compiled in. To use a Swift subclass of the NSManagedObject class with your Core Data model, prefix the class name in the Class field in the model entity inspector with the name of your module.

Walk-through

Create an Entity

In your .xcdatamodeld file, create an Entity to your liking.  In my example, I named the Entity “MyEntity” and I gave it an attribute called “myAttribute” with a data type of String.
Create Entity and Attribute

Create an NSManagedObject Subclass for that Entity

On the Menu, click Editor, then “Create NSManagedObject Subclass…”
Create NSManagedObject Subclass

Make sure you choose “Swift” as your language of choice as you click Next through the wizard and Xcode will generate you a file that is appropriate for your Entity.  The files it created for me (Xcode 7.1.1) look like this:

NSManagedObject Class file

NSManagedObject Properties file

Verify NSManagedObject class in the “Data Model Inspector”

Make sure that you have your .xcdatamodeld file selected in the Navigator panel.  Then make sure your Utilities panel is visible.

Click the “Data Model Inspector” icon.  This will be the last icon in the inspector of Xcode.  You should see a section for “Entity” and within this section, two textboxes:  one for Name and one for Class. You should also see a drop-down for the Module that the NSManagedObject subclass is found in.

You’ll be verifying the Class and the Module values:

Verify class and module

According to the Swift documentation, Swift class namespaces are scoped to the module they’re compiled in (usually the project you’re working in).  

To use the NSManagedObject subclass in your project, you just need to verify that the Module setting is set to “Current Product Module”, assuming that the NSManagedObject subclass you’re wiring this Entity to is found in that module. If it’s in another module, you’ll need to adjust the Module value in the inspector appropriately.

Once the Class and Module values are verified (or set), you’ll be able to use this NSManagedObject subclass anywhere in your project.

comments powered by Disqus