开发者

enum string comparison

开发者 https://www.devze.com 2022-12-17 19:12 出处:网络
I need to compare an enum as a whole to one string, so the whole contents of the enum is checked. Wanted something like:

I need to compare an enum as a whole to one string, so the whole contents of the enum is checked.

Wanted something like:

NSString *colString = [[NSString aloc] initWithString:@"threeSilver"];


typedef enum {
oneGreen,
twoBlue, 
threeSilver
}numbersAndColours;

if (colString == numbersAndColours) {
//Do cool stuff
}

But obviously I can't do that, maybe a struct... sorry, I'm new to C please help?

BTW: I know NSString isn't C, but figure开发者_高级运维d this question was more C, than Obj-C.

Thanks


In C you'd have to write a function for that. It would essentially be a switch statement.

char* colour_of(enum numbersAndColours c)
{
    switch( c ) {
    case oneGreen:
        return "oneGreen";
        break;
    case twoBlue:
        return "twoBlue";
        break;
    /* ... */
    default:
        return "donno";
    }
}

You can use the function then like so:

{
    char* nac;
    nac = colour_of(numbersAndColours);
    if( strncmp(colString, nac, colStringLen) == 0 )
        /* ... */
}

If colString doesn't match any of the enum elements it won't match numbersAndColours. There is no need to compare it against all of the elements.


C, ObjC and C++ don't support that directly, you have to create an explicit mapping.

Example using plain C:

typedef struct { 
    numbersAndColours num;
    const char* const str;
} entry;

#define ENTRY(x) { x, #x }

numberAndColours toNum(const char* const s) {
    static entry map[] = {
        ENTRY(oneGreen),
        ENTRY(twoBlue),
        ENTRY(threeSilver)
    }; 
    static const unsigned size = sizeof(map) / sizeof(map[0]);

    for(unsigned i=0; i<size; ++i) {
         if(strcmp(map[i].str, s) == 0) 
             return map[i].num;
    }

    return -1; // or some other value thats not in the enumeration
}

#undef ENTRY

// usage:

assert(toNum("oneGreen") == oneGreen); 
assert(toNum("fooBar") == -1);

Basic Objective-C approach:

#define ENTRY(x) [NSNumber numberWithInt:x], @#x

NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
    ENTRY(oneGreen),
    ENTRY(twoBlue),
    ENTRY(threeSilver),
    nil];

#undef ENTRY

if([dict objectForKey:@"oneGreen"]) {
    // ... do stuff 
}


I'm not sure I understand what you're trying to achieve, but you may like to have a look into NSSet. It seems like you want your program to do cool stuff if the colString is a particular value.

NSSet *numbersAndColors = [NSSet setWithObjects:@"oneGreen", @"twoBlue", @"threeSilver", nil];
NSString *colString = [[NSString alloc] initWithString:@"threeSilver"];

if ([numbersAndColors containsObject:colString])
{
    // do cool stuff
}

An NSSet is faster than an NSArray when you just want to know whether a particular object exists, but one important aspect about an NSSet is that it does not maintain the order of objects. It is typically used when you don't care about the order and just want to test when an object exists in a set.

0

精彩评论

暂无评论...
验证码 换一张
取 消