Search code examples
objective-ciosplistsandbox

Problem to write into plist


Helle everyone,

I have copied my plist into Sandbox (FileManager) and now I'm able to change plist's values. I'm trying to do that but it doesn't work.

Here is my plist structure :

enter image description here

and my snippet

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"BlogList.plist"];
NSMutableDictionary *ressourceDico = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
NSArray *ressourceArray = [NSArray arrayWithArray:[ressourceDico objectForKey:@"BlogList"]];
for(int i = 0; i < [ressourceArray count] ; i++)
{
    NSMutableDictionary *dico = [ressourceArray objectAtIndex:i];

    if(![[dico objectForKey:@"isSaved"] boolValue] && [[dico objectForKey:@"identifier"] isEqualToString:identifier])
    {
        [dico setObject:[NSNumber numberWithBool:YES] forKey:@"isSaved"];
        [dico writeToFile:plistPath atomically:YES];
    }
}

Solution

  • You can't write just a part of the dictionary into the file, in order to 'update' just a part of it. Instead you have to, write the whole dictionary into the file.
    So I think, you will have to send writeToFile:atomically: after looping over the array elements, but the reciever might be ressourceDico instead of dico.