问题
I have some NSManagedObject classes created for use with CoreData I need to add some additional properties for formatting I am doing using GRMustache templates.
Here is an example property:
-(NSString *) PriceFormatted {
NSNumberFormatter *nfm = [[[NSNumberFormatter alloc] init] autorelease];
[nfm setNumberStyle:NSNumberFormatterCurrencyStyle];
[nfm setCurrencyCode:[Helpers GetCurrencyCode]];
[nfm setNegativeFormat:@"-¤#,##0.00"];
[nfm setMaximumFractionDigits:2];
return [nfm stringFromNumber:self.Price];
}
I currently have this in my generated NSManagedObject class but this will cause issues if I need to regenerate a new NSManagedObject class.
Can I define these properties in a secondary set of classes - similar to partials in C#?
回答1:
Probably the easiest way is to add a category to your generated managed object.
Here is Apple's documentation on it, it is pretty easy.
To quote:
You can add methods to a class by declaring them in an interface file under a category name and defining them in an implementation file under the same name. The category name indicates that the methods are additions to a class declared elsewhere, not a new class. You cannot, however, use a category to add additional instance variables to a class.
The methods the category adds become part of the class type. For example, methods added to the NSArray class in a category are included as methods the compiler expects an NSArray instance to have in its repertoire. Methods added to the NSArray class in a subclass, however, are not included in the NSArray type. (This matters only for statically typed objects because static typing is the only way the compiler can know an object’s class.)
Category methods can do anything that methods defined in the class proper can do. At runtime, there’s no difference. The methods the category adds to the class are inherited by all the class’s subclasses, just like other methods.
The declaration of a category interface looks very much like a class interface declaration—except the category name is listed within parentheses after the class name and the superclass isn’t mentioned. Unless its methods don’t access any instance variables of the class, the category must import the interface file for the class it extends:
#import "ClassName.h"
@interface ClassName ( CategoryName )
// method declarations
@end
Note that a category can’t declare additional instance variables for the class; it includes only methods. However, all instance variables within the scope of the class are also within the scope of the category. That includes all instance variables declared by the class, even ones declared @private.
There’s no limit to the number of categories that you can add to a class, but each category name must be different, and each should declare and define a different set of methods.
回答2:
This is a common issue with CoreData. Check https://github.com/rentzsch/mogenerator, which you can install via https://github.com/mxcl/homebrew.
Mogenerator will produce two classes : one, which contains properties of your data model, which will be recreated each time you update your data model. And another one, for your own methods and categories, which will remain. The perfect container for your GRMustache categories.
BTW, you could check formatting abilities of GRMustache, they may help you as well: https://github.com/groue/GRMustache/blob/master/Guides/NSFormatter.md
来源:https://stackoverflow.com/questions/9900155/add-additional-properties-to-nsmanagedobject-with-secondary-h-m-files