1 Here is one solution for this, but you need to know all the macros:
#define CONSTANT_1 1 #define CONSTANT_2 2 #define CONSTANT_3 3 #define STRINGIZE(x) #x + (int)getValueForContant:(NSString *)constantStr { const char *charStr = STRINGIZE(CONSTANT_1); NSString *str = [NSString stringWithUTF8String:charStr]; if ([constantStr isEqualToString:str]) { return CONSTANT_1; } charStr = STRINGIZE(CONSTANT_2); str = [NSString stringWithUTF8String:charStr]; if ([constantStr isEqualToString:str]) { return CONSTANT_2; } return -1; }
And use it like:
int constVal = [ClassName getValueForContant:@"CONSTANT_1"];
As a result, you get the value int
.
2 Use .plist
to determine the constants and run the loop to get the appropriate value as the provided string parameter to this method;
+ (int)getValueForContant:(NSString *)constantStr
source share