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 figured 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With