views:

358

answers:

4

What is the best way to add custom methods to my core data generated classes?

For example, say I have a "Person" entity with properties "firstname" and "lastname". I wish to add a "fullname" method, which returns a concatenation of the firstname and lastname properties.

I could add the method to the generated .h and .m files, but this would be difficult to maintain during development when my entities may still change. Recreating the .h and .m file would overwrite these changes. Another idea is to subclass the generated class and add the methods there.

Is there a better way?

A: 

You create your own subclass of NSManagedObject for the entities.

Better explained in the NSManagedObject documentation

Abizern
I'm using an .xcdatamodel file, which generates the Managed Object classes for me. My question is about further customizing this generated class (which already is a subclass of NSManagedObject) with custom methods, such as the example in my original post.Thanks!
chris
Look at your data modeller. at the top left hand side it lists the entities. It also lists the class that it's based on, which by default is NSManagedObject. You can change that to the name of your own NSManagedObjectS subclass.
Abizern
I must be misunderstanding something. If I create an NSManagedObject subclass and use that as a base for my generated classes, then I have this hierarchy: NSManagedObject :: Person :: PersonMO. The PersonMO class contains my attributes, and the Person class contains my methods. However, the PersonMO attributes are not visible to the Person methods.
chris
I think you are misunderstanding. The Hierarchy is NSManagedObject < PersonMO and Person is an object of the PersonMO class.
Abizern
+2  A: 

I find that the best way to add custom methods that aren't directly tied to data properties is to use a category. This generally works best if you create your Core Data entities as their own subclasses of NSManagedObject in the data modeler, but it can work without that as well. This way all the machine generated code can stay in the main .h and .m files, and all your custom code goes in the .h and .m for your category on that class.

Shawn Craver
+1. By using categories, you can blow away and rebuild the Xcode-generated entity header and implementation as you change the model, while still keeping your own custom methods untouched and safe.
Alex Reynolds
+1  A: 

I'd recommend adding these methods to your custom NSManagedObject subclass. If you're worried about maintaining accessors as your data model changes, while preserving your custom methods, I'd suggest looking to "Wolf" Rentzsch's mogenerator. Many people swear by this tool for just this purpose.

Brad Larson
A: 


Add a method to your class as you would any other subclass you make. It might look like this:

// in your .h file

- (NSString *)fullname;


// in your .m file

- (NSString *)fullname {
  return [NSString stringWithFormat:@"%@ %@", self.firstname, self.lastname];
}
imnk