Super useful NSNumber/enum category!

I found a brilliant way of dealing with enums: an NSNumber category!

It’s especially useful when you have a Core Data table with an attribute representing an enum!

typedef enum {
} EntityTypeEnum;

@interface NSNumber (NSNumberEnum)
+ (NSNumber *)numberWithEntityType:(EntityTypeEnum)entityType;
- (EntityTypeEnum)entityTypeValue;

@implementation NSNumber (NSNumberEnum)

- (EntityTypeEnum)entityTypeValue {
int intValue = [self intValue];
NSAssert(intValue >= 0 && intValue <= 3,
@"unsupported entity type");
return (EntityTypeEnum)intValue;

+ (NSNumber *)numberWithEntityType:(EntityTypeEnum)entityType {
return [NSNumber numberWithInt:(int)entityType];


This entry was posted in Uncategorized. Bookmark the permalink.

One Response to Super useful NSNumber/enum category!

  1. Sean says:

    Hi there, this looks pretty good. Can you post an example of how you would then use this in a NSManagedObject class?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s