Dominik Hadl
Dominik Hadl

Reputation: 3619

Get macro value from NSString containing macro name

I have an array full of #define macros values. I have also a plist file (dictionary) with dictionaries, each named the same as the #define macros name. I get the the names of the dictionaries from the plist files and have them in NSString. Now, I need to convert the string (equal to the name of a macro) to the actual macro value. Then I need to compare the array full of the values against the values of the macros specified in the .plist file and check if it is equal. See code for example.

How can I do that?

    #define ACHIEVEMENT1_ID (ISIPAD() ? @"com.company.blablaHD.id" : @"com.company.blabla.id")
    #define ACHIEVEMENT2_ID ....
    ......

    - (void)checkPlistAndKnownIdentifiers:(NSDictionary *)_plistDictionary {

        NSArray *_knownIdentifiers = @[ACHIEVEMENT1_ID, ACHIEVEMENT2_ID, ...];

        NSArray *_plistDictionariesNames = [_plistDictionary allKeys];

        for (int idx = 0; idx < [_knownIdentifiers count]; idx++) {

           if (_knownIdentifiers[idx] == _plistDictionariesNames[idx]){
               // _knownIdentifiers[idx] returns @"com.company.blabla.id", for example
               // but _plistDictionariesNames[idx] returns @"ACHIEVEMENT1_ID", that is the problem
           } else {
               assert(NO);
           }

        }

    }

There might be some errors, as I am writing this from memory, but you should get the general idea.

Also, I don't want to use extern const strings here, so that's the reason I am asking for help :)

Upvotes: 2

Views: 517

Answers (1)

Antzi
Antzi

Reputation: 13424

If I understand your question; you want to be able to use a macro name at run time. This is actually impossible, as preprocessor compute the macro, and replace it before actual compilation. Your macro name does not exist anymore at runtime

Upvotes: 3

Related Questions