Using Extensions to add functionality to NSManagedObject classes

Posted by jaisonv on August 5, 2015

When I started programming in Objective-C I didn’t know all the power this language had. It’s common for me to learn by doing, and when I have doubts I google about it until I finish what I am doing. After giving my first steps I like reading a book to know everything the language can offer. When I read the book about Objective-C I found out the existence of Protocols and Categories and liked it a lot. I didn’t know of anything like this in Java.

I will not talk about Objective-C here, there might be tons of contents about this language on the web. And since this year’s WWDC I motivated myself to learn Swift.

In Swift we have the Extensions, which is the equivalent to Categories in Objective-C.

From Swift’s documentation:

“Extensions can add new functionality to a type, but they cannot override existing functionality.”

Working with Coredata, Xcode does all the work creating the NSManagedObject classes based on entities. But sometimes you need to add methods to these classes, and it’s annoying to add these methods again every time you modify the entities and use Xcode to recreate the NSManagedObject classes. That’s a good place where we can take advantage of Extensions. Let’s see how it works.

Suppose we have an entity called “Person” having the properties “name” and “age”. Xcode will the following NSManagedObject file named Person.swift:

import Foundation
import CoreData

class Parent: NSManagedObject {
    @NSManaged var name: String
    @NSManaged var age: NSNumber
}

This way we create a Swift class to implement the Extension (I will name it PersonExtension.swift):

extension Person {

    func sayMyName {
        print(name)
    }
}

Now we can create objects and use its methods.

let heisenberg = NSEntityDescription.insertNewObjectForEntityForName("Person", inManagedObjectContext: appDelegate.managedObjectContext) as! Person
heisenberg.name = "Heisenberg"
heisenberg.age = 51

heisenberg.sayMyName()

This only one of the many ways we can use Extensions. It is up to your creativity to think about how to use it in your project.


If you liked this post click here to share it on Twitter. Jaison Vieira jaisonv.com