5

Possible Duplicate:
Convert objective-c typedef to its string equivalent

I have an enum declared as followed:

typedef enum MODE {
    FRAMED, HALFPAGED, FULLPAGED
} MODE;

Is there any way to convert the FRAMED/HALFPAGED/FULLPAGED to a string.

I know C++ has the ability by using:

static String^ GetName(
    Type^ enumType,
    Object^ value
)

Would there be an equivalent for Objective-C?

Community
  • 1
  • 1
Thomas
  • 649
  • 9
  • 17

2 Answers2

8

You can implement a method like this:

- (NSString*)modeToString:(MODE)mode{
    NSString *result = nil;
    switch(mode) {
        case FRAMED:
            result = @"FRAMED";
            break;
        case HALFPAGED:
            result = @"HALFPAGED";
            break;
        case FULLPAGED:
            result = @"FULLPAGED";
            break;
        default:
            [NSException raise:NSGenericException format:@"Unexpected MODE."];
    }
    return result;
}
Giorgio
  • 1,973
  • 4
  • 36
  • 51
0

As far as i'm aware, there isn't a built in way to do what you're asking.

My approach would be something like:

- (NSString *)modeString:(MODE)mode
{
    if(mode == FRAMED)
    {
        return @"FRAMED";
    }
    else if(mode == HALFPAGED)
    {
        return @"HALFPAGED";
    }

    return @"FULLPAGED";
}
Zack Brown
  • 5,990
  • 2
  • 41
  • 54