0

I have about 200 String values and each string needs to be placed into 1 of 20 arrays (sometimes more than 1). The data is all static and I know ahead of time which array I want each string to be placed into.

I'd like to create a plist to hold my strings. Each entry in the plist is a key-value dictionary. One key-value will be the string itself. But I also want to add an array to each dictionary, and the array will contain a list of the name of each array that the string should be placed into.

So I'm wondering, is there a way to reference a variable from my plist? I'd like to be able to just add a string into the plist with a value that is the name of my array variable. At some later time from my code, I want to retrieve that string from the plist and convert it into a reference to the variable with the same name. Any ideas how to do this?

Thanks!

Sunny

1 Answer 1

1

You need a dictionary that maps an "array name" to the array object. For example:

NSMutableArray *redThings = [NSMutableArray array];
NSMutableArray *greenThings = [NSMutableArray array];
NSMutableArray *blueThings = [NSMutableArray array];

NSDictionary *arraysByName = [NSDictionary dictionaryWithObjectsAndKeys:
    redThings, @"redThings",
    greenThings, @"greenThings",
    blueThings, @"blueThings",
    nil];

NSArray *plist = loadThePropertyList();

for (NSDictionary *item in plist) {
    NSString *theStringItself = [item objectForKey:@"theStringItself"];
    NSArray *arrayNames = [item objectForKey:@"arrayNames"];
    for (NSString *arrayName in arrayNames)
        [[arraysByName objectForKey:arrayName] addObject:theStringItself];
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.