Creating string enumerations in Objective-C (The ultimate solution)

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.

About these ads

3 thoughts on “Creating string enumerations in Objective-C (The ultimate solution)

  1. 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

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s