how to store a reference to, and then use extern NSString const?

209 views Asked by At

i've made a plist as a dictionary with dictionary entries, one for each ABRecord property that i want to use later.

these dictionaries are key/value NSStrings like this: kABEmailProperty/email.

i want to be able to use the unpacked plist to gather values from a specified ABRecord by enumerating the unpacked plist dictionary (assume inRecord is an ABRecord):

__block NSMutableArray *valueGroup = [[NSMutableArray alloc] init];

[self.propsDict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        NSString *valueString = [inRecord valueForProperty:key];
        if (valueString) { 
            NSDictionary *dict = [NSDictionary dictionaryWithObject:valueString forKey:obj];
            [valueGroup addObject:dict];    
                }
            }
        }];

how do i properly refer to key, so that it is the ABRecord global property reference in ABGlobals.h instead of the string that is stored in the plist ?

i've tried using &key, and (id)key, and (void *)key, but i'm really just flailing around.

i am unsuccessful in searching through the various questions regarding using global externs, although that has been informative.

1

There are 1 answers

2
Jesse Rusak On BEST ANSWER

If I understand you correctly, you've got the following:

NSString *foo = "kABEmailProperty";

And you want to be able to do something like this:

[inRecord valueForProperty:kABEmailProperty];

Based on the value of foo. Correct? There's no way to do this directly, because the name "kABEmailProperty" isn't (easily) accessible at runtime; it's just a compile-time name.

My suggestion would be to make a dictionary with all keys in it; something like this:

NSDictionary *keys = [NSDictionary dictionaryWithObjectsAndKeys:kABEmailProperty, @"kABEmailProperty", kABOtherProperty, @"kABOtherProperty", ..., nil];

And then you can do something like this:

[inRecord valueForProperty:[keys objectForKey:foo]];

i.e. Make a mapping from the string @"kABEmailProperty" to the value that kABEmailProperty actually has.