Core Data: Subclassing NSManagedObject
So far, after making Entities in our Data Model Editor, we instantiate NSManagedObjects using the Entity’s EntityDescription. Now that’s fine and dandy but there are some issues with this approach.
First off, when we want to set the attributes of the Managed Object, we can’t use dot syntax like we would when we set the properties of regular objects. Dot syntax is way cleaner and way more convenient than the setValue:forKey: method we’ve been using. Here are two reason why you would want to use dot syntax instead
- When we use the value-key way of setting our attributes, we can pass in an any type of value. This can be dangerous because we could accidentally pass in a NSDate for an NSString and the compiler wouldn’t complain until the app crashes. With dot syntax, the compiler will throw us an error or warning when we put in the wrong type saving us from potential headaches.
- When we get our attribute values using the valueForKey: method, we get an object of id type. To actually use that value, we’ll have to remember what type of object it is and then cast it accordingly. This can be a real pain to deal with when your project gets fairly large.
Another issue that we get is that we can’t give custom behavior to our entities. So we can’t give our Managed Objects custom methods.
So what we’re going to do is to make a subclass of our entity. So let’s make a subclass of our Dog entity. We start by going to our Data Model Editor, selecting the Entity we want to subclass, and then go to Editor > Create NSManagedObject Subclass.

It’ll ask you for which Data Model holds the Entity (or Entities) you plan to subclass. Then it’ll give you a checklist of Entities and ask you which ones you want to subclass. If you selected a Entity before you attempted to create a subclass, that Entity will be checked by default.

So go ahead and create it. Lo and behold, you get 4 files!

So what we ended up getting was a Dog class and a Dog+CoreDataProperties extension. If you open the Dog.h file, you’ll notice it’s empty. It also says:
// Insert code here to declare functionality of your managed object subclass
So cool, so we add our custom methods and properties we don’t need persisted here in the Dog class.
Now if you open the Dog+CoreDataProperties.h file, you’ll see this:

Huh. Those are all our attributes. They were turned into properties. Interesting.
So the way this works is, anything custom we want to add goes into the Dog class. Anything that Core Data is managing goes into the Dog extension. Typically you wouldn’t really mess with that part.
You might be wondering why Core Data didn’t just put everything in the Dog class. Well, if you ever change the attributes and need to update something about the Entity in the Data Model Editor, you might notice that Core Data doesn’t automatically change the Dog+CoreDataProperties files. To have it reflect the changes, you have to make a new subclass of the Entity which will overwrite the extension only. So basically, anything you put in the extension would be lost for good. So Core Data works around this problem by only overwriting the extension and leaving the class file for you to put your code in.
Cool. So let’s just add a custom method because why not.


Cool. So we’ll be checking if this works soon enough.
So now back to the first proposal: How the hell do we get our damn dot syntax? Well, simple. We instantiate our Dog object by instantiating the NSManagedObject like we did before but we downcast it to a Dog object. Lastly, we save the returned Dog in a Dog variable.

Awesome, it works! Not only that, but as you can see the compiler is complaining since we put the wrong type in for weight. That’s one headache avoided.
Lastly, let’s see if that custom method of ours worked. And …

Awesome, it works! So now for the TLDR …
TLDR
- Go to Data Model Editor
- Go to Editor > Create NSManagedObject Subclass
- Choose the Data Model and then the Entities you want to subclass
- Optionally add custom behavior
- To instantiate your custom object, simply downcast your NSManagedObject
- Now you can use dot syntax
- Profit!!!
Cool, so that’s how you make an NSManagedObject Subclass.