Question

This is how I am accessing the dictionary in the plist in my viewDidLoad method:

     NSString* documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
     NSString *fileName = [NSString stringWithFormat:@"Level.plist"];
     filePath = [documentsDir stringByAppendingPathComponent:fileName];
     array = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
     dict = [array objectAtIndex:1];

This works fine, I then write to the dictionary like this:

    score = [NSString stringWithFormat:@"%d", score.integerValue + 10];
    [dict setObject:score forKey:@"Score"];
    [dict writeToFile:filePath atomically: NO];

This works fine too, however once I return to this view and try to access the dictionary again in the viewDidLoad method it returns (null) for the dictionary.

Was it helpful?

Solution

This is because you are reading the file as an array, but then when you write it back out again, you're only writing out the dictionary, not the array that contains that dictionary. If you try to use NSMutableArray to read a plist file that has something other than an array at its root, that will result in it returning nil. So if you just replace dict with array in the last line of code, that should do the trick.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top