0

I have a named enumeration

enum{
    MyErrorCodeOne            = 1
    MyErrorCodeTwo            = 2
}MyErrorCodes;

Is there a way when I encounter 2 for instance that I NSLog "MyErroCodeOne" to the user as opposed to 2. A switch statement is not the case because I have about 50 error codes at the moment. Any help would be appreciated.

John Lane
  • 1,112
  • 1
  • 14
  • 32
  • possible duplicate of [How to convert enum names to string in c](http://stackoverflow.com/questions/9907160/how-to-convert-enum-names-to-string-in-c) – Joe May 09 '12 at 14:17
  • This is what I'm trying to avoid, having to do a list of 50+ elements. Is there a way to pass a variable name as a string. – John Lane May 09 '12 at 14:24
  • More examples [Print text instead of value from C enum](http://stackoverflow.com/questions/3168306/print-text-instead-of-value-from-c-enum), [How to easily map c++ enums to strings](http://stackoverflow.com/questions/207976/how-to-easily-map-c-enums-to-strings), [Easy way to use variables of enum types as string in C?](http://stackoverflow.com/questions/147267/easy-way-to-use-variables-of-enum-types-as-string-in-c). Remember Objective-C is a superset of C. – Joe May 09 '12 at 14:25

1 Answers1

0

The comment response is good, but you could also just have an array defined globally, say error names like so:

NSArray *errorNames = [NSArray arrayWithObjects:@"MyErrorCodeOne", @"MyErrorCodeTwo", nil];

and reference it by [errorNames objectAtIndex:MyErrorCodeOne];

Only if that's easier for you. The first response would be the best, in my opinion.

RileyE
  • 10,874
  • 13
  • 63
  • 106