A while ago I wrote on my blog about a solution to one of the most common questions asked by Objective-C programmers which is “How can I create string enumerations?”. Well, the solution that I’ve given has immediately become one of the top subjects that attracts developers to my blog, as I can see in my stats. I thought I should now take it to a whole other level and get rid of the limitations that I had presented in the old solution, and come up with a fresh perspective.
The following video is the result of my work on this subject. I hope you’ll enjoy watching it.
Hey, nice solution. I was playing with it and thought that if someone wants to have different start value in NSEnum you can just specify it like that :
static const NSInteger startIndex = 5;
typedef NS_ENUM(NSInteger, Name) {
NameAndrew = startIndex,
NameCaty,
NameSara,
};
NSString *const kNameAndrew = @”Andrew”;
NSString *const kNameCaty = @”Caty”;
NSString *const kNameSara = @”Sara”;
@interface AppDelegate ()
@end
@implementation AppDelegate
– (NSString *) stringForName:(Name) name
{
__strong NSString ** pointer = (NSString **)&kNameAndrew;
pointer += name – startIndex;
return *pointer;
}
Now it works as intended. Nice tricks.
Andrew
I don’t see any real advantage of this over having an array of NSString and indexing into it. This seems much more fragile.