vendredi 8 mai 2015

Cant write data to plist file in - objective c

I'm trying to read and write data to my plist file, While the reading part goes well, the writing part does nothing.

I might be mistaken for the writing part - i can't see any changes in my file under my bundle - it is still empty after my changes, and when i close the app and open it again - i still see empty email address line.

The code for writing (and placing the plist in the document folder for future writings)

NSFileManager *fileManger=[NSFileManager defaultManager];
NSError *error;
NSArray *pathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *doumentDirectoryPath=[pathsArray objectAtIndex:0];
NSString *destinationPath= [doumentDirectoryPath stringByAppendingPathComponent:@"userData.plist"];

NSLog(@"plist path %@",destinationPath);
if ([fileManger fileExistsAtPath:destinationPath]){
    NSLog(@"database localtion %@",destinationPath);
    //return;
}

NSString *sourcePath = [[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:@"userData.plist"];
[fileManger copyItemAtPath:sourcePath toPath:destinationPath error:&error];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile: sourcePath];
NSString *emailAddress = (NSString *)[dict objectForKey: @"emailAddress"];
if([emailAddress isEqualToString:@""])
{
    // Do stuff
}

And for writing

NSArray *pathsArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);

NSString *doumentDirectoryPath =[pathsArray objectAtIndex:0];

NSString *destinationPath = [doumentDirectoryPath stringByAppendingPathComponent:@"userData.plist"];

NSMutableDictionary *plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile: destinationPath];

[plistDict setValue:@"myEmail@gmail.com" forKey:@"emailAddress"];
[plistDict writeToFile:destinationPath atomically: YES];

But as i said, nothing is changed in the file itself, and not even when i save, close the app and open it again (the string is always empty on my reading part)

Any help will be more than welcomed.

Aucun commentaire:

Enregistrer un commentaire