Skip to content

Add convenience methods involving the entity name to machine generated classes #9

adib opened this Issue Jan 4, 2010 · 7 comments

4 participants

adib commented Jan 4, 2010


Would you be so kind to add these convenience methods to the machine-generated classes? I know that I should be able to add these myself to the *.motemplate files but I fear that if I do that it'll break my code when I upgrade mogenerator.

/* This is for type safety so that the code doesn't break in case the entity name is edited in the data model file. */
+(NSString*) entityName;

/* This is to be used for custom fetches. */
+(NSEntityDescription*) entityDescriptionInManagedObjectContext:(NSManagedObjectContext*) ctx;

Thank you.

rentzsch commented Jan 4, 2010

Save me 15 minutes: do you have implementations of these methods?

seanm commented Jan 5, 2010

2¢ : Such methods can be added as categories on NSManagedObject, which I for one have done. With the exact same method names, I believe. That would cause a conflict (which implementation gets used is undefined IIRC). I'm undecided if I'd find this change disagreeable. I guess I could remove them from my category, but then I'd no longer be able to use those methods on plain NSManagedObjects.

adib commented Jan 5, 2010

@rentzsch Sure. Here you go:

+(NSString*) entityName {
    return @"<$name$>";

+(NSEntityDescription*) entityDescriptionInManagedObjectContext:(NSManagedObjectContext*) ctx {
    return [NSEntityDescription entityForName:@"<$name$>" 

I hope the method names suits you.


On your additions to NSManagedObject, how do you pass the entity name strings? (the names you define in XCode data modeler). The last time I checked, you'll need either

a) a live instance of NSManagedObject and then call [-managedObjectClassName] (note that this is a non-static method.

b) Type in the entity name yourself (which you may need to update in the Objective-C source file whenever you change the entity name in XCode Data Modeler.

My suggestion is essentially going with option b) but the entity name is "typed in" by mogenerator.


seanm commented Jan 5, 2010

Now that I'm in front of my code....

@implementation NSManagedObject (MyCategory)

  • (NSString*)entityName { return [[self entity] name]; }


What's the 'entityDescriptionInManagedObjectContext:' method for? Does it not do the same as the 'entity' method?


adib commented Jan 17, 2010

The "+(void) entityDescriptionInManagedObjectContext:" is a static method - useful if you don't have a "running" instance of the object just yet (e.g. for fetching those objects).


Hi there,
I have another request for similar convenience methods for property names/keys. E.g. when you need to make an NSSortDescriptor when fetching objects:

NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES] autorelease];

It would be nice if that @"title" key would be a class method of the objects I am fetching. So that you would get something like:

NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:[MyManagedObject titleKey] ascending:YES] autorelease];

They might also be defined as "constants", e.g. in the generated _MyManagedObject.h:

extern NSString* MyManagedObjectTitleKey;

and in _MyManagedObject.m:

NSString* MyManagedObjectTitleKey = @"title";

But I think the first alternative is preferable.


rentzsch commented Jul 1, 2010

I think 1.17 solved this?

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.