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 {
EntityTypeOrganisation,
EntityTypeDepartment,
EntityTypeFunction,
EntityTypeTeam
} EntityTypeEnum;
@interface NSNumber (NSNumberEnum)
+ (NSNumber *)numberWithEntityType:(EntityTypeEnum)entityType;
- (EntityTypeEnum)entityTypeValue;
@end
@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];
}
@end
Hi there, this looks pretty good. Can you post an example of how you would then use this in a NSManagedObject class?