Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-c enum object to string [duplicate]

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?

like image 656
Thomas Avatar asked Jun 12 '12 08:06

Thomas


1 Answers

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;
}
like image 160
Giorgio Avatar answered Oct 24 '22 13:10

Giorgio