Objective-c enum对象字符串[duplicate]

时间:2022-09-07 09:37:48

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

可能重复:将objective-c类型def转换为它的字符串等效

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:

我知道c++有以下能力:

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

Would there be an equivalent for Objective-C?

Objective-C会有等价的吗?

2 个解决方案

#1


7  

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;
}

#2


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";
}

#1


7  

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;
}

#2


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";
}