Search code examples
objective-cplistnsdictionary

Editing a plist in NSDictionary


I'm importing a plist into an NSDictionary object and want to change the contents of the plist once in the dictionary to lowercase. I have tried several different ways with no luck. Maybe I am supposed to edit the strings that are inside the NSDictionary, but then I don't know how to do that.

What I have currently imports the plist correctly, but the lowercaseString line effectively does nothing.

NSString *contents = [[NSBundle mainBundle] pathForResource:@"Assets/test" ofType:@"plist"];

NSString *newContents = [contents lowercaseString];

NSDictionary *someDic = [NSDictionary dictionaryWithContentsOfFile:newContents];

for (NSString *someData in someDic) {
        NSLog(@"%@ relates to %@", someData, [someDic objectForKey:someData]);
    }

I NSLog'd the "content" string and it gave me a path value, so obviously changing that to a lowercaseString wouldn't do anything. I'm feeling like I have to access the strings within the dictionary.


Solution

  • This line

    NSString *newContents = [contents lowercaseString];
    

    is change the path string returned from

    NSString *contents = [[NSBundle mainBundle] pathForResource:@"Assets/test" ofType:@"plist"];
    

    so you will end up with something like ../assets/test.plist

    you will need to walk through the contents of someDic an create a new dictionary based on the old turning strings into lowercase, if you are only concerned about values directly in someDic you can do something like

    for( NSString * theKey in someDic )
    {
        id     theObj = [someDic objectForKey:theKey];
        if( [theObj isKindOfClass:[NSString class]] )
            theObj = [theObj lowercaseString];
        [theNewDict setObject:theObj forKey:theKey];
    }