enum Values to NSString (iOS) enum Values to NSString (iOS) ios ios

enum Values to NSString (iOS)


I didn't like putting the enum on the heap, without providing a heap function for translation. Here's what I came up with:

typedef enum {value1, value2, value3} myValue;#define myValueString(enum) [@[@"value1",@"value2",@"value3"] objectAtIndex:enum]

This keeps the enum and string declarations close together for easy updating when needed.

Now, anywhere in the code, you can use the enum/macro like this:

myValue aVal = value2;NSLog(@"The enum value is '%@'.", myValueString(aVal));outputs: The enum value is 'value2'.

To guarantee the element indexes, you can always explicitly declare the start(or all) enum values.

enum {value1=0, value2=1, value3=2};


This is answered here: a few suggestions on implementation

The bottom line is Objective-C is using a regular, old C enum, which is just a glorified set of integers.

Given an enum like this:

typedef enum { a, b, c } FirstThreeAlpha;

Your method would look like this:

- (NSString*) convertToString:(FirstThreeAlpha) whichAlpha {    NSString *result = nil;    switch(whichAlpha) {        case a:            result = @"a";            break;        case b:            result = @"b";            break;        case c:            result = @"c";            break;        default:            result = @"unknown";    }    return result;}


I will introduce is the way I use, and it looks better than previous answer.(I thinks)

I would like to illustrate with UIImageOrientation for easy understanding.

typedef enum {    UIImageOrientationUp = 0,            // default orientation, set to 0 so that it always starts from 0    UIImageOrientationDown,          // 180 deg rotation    UIImageOrientationLeft,          // 90 deg CCW    UIImageOrientationRight,         // 90 deg CW    UIImageOrientationUpMirrored,    // as above but image mirrored along other axis. horizontal flip    UIImageOrientationDownMirrored,  // horizontal flip    UIImageOrientationLeftMirrored,  // vertical flip    UIImageOrientationRightMirrored, // vertical flip} UIImageOrientation;

create a method like:

NSString *stringWithUIImageOrientation(UIImageOrientation input) {    NSArray *arr = @[    @"UIImageOrientationUp",            // default orientation    @"UIImageOrientationDown",          // 180 deg rotation    @"UIImageOrientationLeft",          // 90 deg CCW    @"UIImageOrientationRight",         // 90 deg CW    @"UIImageOrientationUpMirrored",    // as above but image mirrored along other axis. horizontal flip    @"UIImageOrientationDownMirrored",  // horizontal flip    @"UIImageOrientationLeftMirrored",  // vertical flip    @"UIImageOrientationRightMirrored", // vertical flip    ];    return (NSString *)[arr objectAtIndex:input];}

All you have to do is :

  1. name your function.

  2. copy contents of enum and paste that between NSArray *arr = @[ and ]; return (NSString *)[arr objectAtIndex:input];

  3. put some @ , " , and comma

  4. PROFIT!!!!