C, ObjC and C ++ do not support this directly, you need to create an explicit mapping.
An example of 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;
The 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"]) {
source share