writeToFile:atomically: not saving new plist data

I am using writeToFile:atomically: to update the value of a key in my plist by 1 every time the app is launched. I put this code in viewDidLoad, which reads the string value of the key, gets the numeric value of that string, increases it by 1, converts it back to a string, and writes that as the new string for that key, but when I read it again it seems to have not updated. I can't figure out what I'm doing wrong. I don't need a special framework for writeToFile:atomically:, do I?

Here is the code:

NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *path = [documentsDirectory stringByAppendingPathComponent:@"DaysLaunched.plist"];

NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath: path])
{
    NSString *bundle = [[NSBundle mainBundle] pathForResource:@"DaysLaunched" ofType:@"plist"];

    [fileManager copyItemAtPath:bundle toPath:path error:&error];
}

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

NSMutableDictionary *data1 = [[NSMutableDictionary alloc] initWithContentsOfFile: path];

NSString *currentNumberOfDays = [NSString stringWithFormat:@"%@",[plistDict objectForKey:@"numberOfDays"]];
NSLog(@"currentNumberOfDays = %@", currentNumberOfDays); //0

int days = [currentNumberOfDays intValue];
days ++;
currentNumberOfDays = [NSString stringWithFormat:@"%d", days];

[data1 setObject:[NSString stringWithFormat:@"numberOfDays"] forKey:currentNumberOfDays];
NSLog(@"currentNumberOfDays = %@", currentNumberOfDays); //1


[data1 writeToFile: path atomically:YES];

currentNumberOfDays = [NSString stringWithFormat:@"%@",[plistDict objectForKey:@"numberOfDays"]];
NSLog(@"currentNumberOfDays = %@", currentNumberOfDays); //0 ??????? writeToFile isn't working?

And here is a screenshot of "DaysLaunched.plist" in my 'Supporting Files' folder, (I've also verified that the plist file name is spelled exactly the same way I spelled it in my code, via Copy-Paste)

The original plist file is also in my 'Copy Bundle Resources' in targets.

Answers


You have made a mistake while setting your data.

You should do:

[data1 setObject:currentNumberOfDays forKey:@"numberOfDays"];

instead of:

[data1 setObject:[NSString stringWithFormat:@"numberOfDays"] forKey:currentNumberOfDays];

Try

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *path = [documentsDirectory stringByAppendingPathComponent:@"DaysLaunched.plist"];

NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath: path])
{
    NSString *bundle = [[NSBundle mainBundle] pathForResource:@"DaysLaunched" 
                                                       ofType:@"plist"];
    [fileManager copyItemAtPath:bundle toPath:path error:&error];
}

NSMutableDictionary *plistDict = [NSMutableDictionary dictionaryWithContentsOfFile:path];

NSInteger days = [plistDict[@"numberOfDays"] integerValue];

plistDict[@"numberOfDays"] = [@(++days) stringValue];

[plistDict writeToFile: path atomically:YES];

Need Your Help

RPC 1.0 vs deRPC GWT-RPC

gwt gwt-rpc

I'm reading a book about GWT (Google Web Toolkit) where the author quote the differences between RPC 1.O and deRPC.

Get RStudio Viewer pane dimensions programmatically

r rstudio

Is it possible to get current RStudio Viewer pane dimensions programmatically?