I have a bunch of constants declared like this:
#define kConstant0 @"Cow"
#define kConstant1 @"Horse"
#define kConstant2 @"Zebra"
Elsewhere in code I’m trying to extract the constant value by adding an integer to the string name of the constant:
int myInt = 1; // (Actual intValue Pulled From Elsewhere)
myLabel.text = [@"kConstant" stringByAppendingString:[NSString stringWithFormat:@"%i",myInt]];
But of course this returns:
myLabel.text = @"kConstant1";
When I want it to return:
myLabel.text = @"Horse";
I can’t figure out how to convert the NSString @”kConstant1″ into the constant name kConstant1.
Any help is appreciated.
lq
You can’t do it automatically. You have to store the mapping in an NSDictionary, e.g.
If all those constants are of the form
kConstantN, it is better to just create an array.