Updating and saving data in plist
Asked Answered
B

2

6

I am making an iOS game and need to save the highest level the player has reached. I can successfully change a data element in a plist, but for some reason, that data keeps reverting to its original value every time the game restarts. Here is the basic flow of my code:

In the game's init, get the highest level the player has reached (the original value is 1)

pData = [[PlayerData alloc] init];
currentLevel = [[pData.data valueForKey:@"Highest Level"] intValue];
[self startNewLevel:currentLevel];

'data' is an NSMutableDictionary that gets initialized like this in PlayerData:

self.data = [NSMutableDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"playerdata" ofType:@"plist"]];

later, if the player beats the highest level, I increment the 'highest level' value and write to the file by calling this function in PlayerData:

-(void) newHighestLevel:(NSString*)path :(int)level{
     [self.data setValue:[NSNumber numberWithInt:level] forKey:path];
     [self.data writeToFile:@"playerdata.plist" atomically:YES]

I know all this is working because I have a level menu that the player can access while playing the game. Each time the player presses the level menu button, a subclass of a UITableView gets created that displays level 1 through the highest level reached. The initialization looks like this:

levelMenu = [[LevelMenu alloc] init:[[pData.data valueForKey:@"Highest Level"] intValue]];

The level menu displays the correct number of levels while in the game (e.g. if the player hasn't beat any levels and goes to the level menu, it just displays "level 1"; but if the player beats level 1 and goes to the level menu, it displays "level 1" and "level 2"). However, whenever the app gets terminated or the user quits to the games main menu, the value for 'highest level' reverts back to 1 and this code:

currentLevel = [[pData.data valueForKey:@"Highest Level"] intValue];

always sets currentLevel to 1 when the player pushes start, no matter how many levels the user beat while playing.

Why is the value changing back? Am I missing something that would make my plist edits permanent?

EDIT:

here is my new 'newHighestLevel' method:

-(void) newHighestLevel:(NSString*)path :(int)level{
    [self.data setValue:[NSNumber numberWithInt:level] forKey:path];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    NSString *docfilePath = [basePath stringByAppendingPathComponent:@"playerdata.plist"];
    [self.data writeToFile:docfilePath atomically:YES];
    self.data = [NSMutableDictionary dictionaryWithContentsOfFile:docfilePath];
    BOOL write = [self.data writeToFile:docfilePath atomically:YES];
}

write gets set to YES. If I change 'docfilePath' to @"playerdata.plist", it gets set to NO. Nothing seems to change with the game in either case.

SOLUTION:

-(void) newHighestLevel:(NSString*)path :(int)level{
      [self.data setValue:[NSNumber numberWithInt:level] forKey:path];
      NSString *basePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
      NSString *docfilePath = [basePath stringByAppendingPathComponent:@"playerdata.plist"];
      [self.data writeToFile:docfilePath atomically:YES];
}

and in init

-(id) init{

     NSString *basePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
     NSString *docfilePath = [basePath stringByAppendingPathComponent:@"playerdata.plist"];
     NSFileManager *fileManager = [NSFileManager defaultManager];
     if (![fileManager fileExistsAtPath:docfilePath]){
         NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"playerdata" ofType:@"plist"];
         [fileManager copyItemAtPath:sourcePath toPath:docfilePath error:nil];
     }
     self.data = [NSMutableDictionary dictionaryWithContentsOfFile:docfilePath];
     return self;
}
Boliviano answered 5/9, 2012 at 4:33 Comment(4)
If you want all the developers you work with to hate you and curse your name forever, keep using unnamed arguments in your method signatures :) As for your question, what value does the [self.data writeToFile:@"playerdata.plist" atomically:YES] call return? My suspicion is that's a read-only file and the write is failing but you're not catching the error.Bainter
It returns NO. Is there a way to make the plist writable? or another data file that isn't read only?Boliviano
I think Prince's answer should work for you.Bainter
If you're just storing the highest level completed, you could just store it in NSUserDefaults.Silma
K
13

The easiest way to create a dictionary from a plist is to use the method dictionaryWithContentsOfFile:,

For example, to load a plist from your resources:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"playerData" ofType:@"plist"];
NSMutableDictionary *plistdict = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];

Writing a plistdict is equally simple:

[plistdict writeToFile:filePath atomically:YES];

Note that you can't write into your app's resources, so you'll have to create a different path, e.g. in your Documents directory for your plist.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString *docfilePath = [basePath stringByAppendingPathComponent:@"playerData.plist"];
[plistdict writeToFile:docfilePath atomically:YES];

Now retrieve data from plist again.

 NSMutableDictionary *plistdict = [NSMutableDictionary dictionaryWithContentsOfFile:docfilePath];

Add your content in plistDict and write it again.

Kee answered 5/9, 2012 at 4:43 Comment(4)
I tried your solution, the last plist write is returning true, but the problem isn't solved - the game still reverts back to level 1 on restart.Boliviano
@CBas loading this same file and not the one from your bundle?Bainter
the iPad is disconnected, its not reloading the bundle.. Do you mean I need to change my init?Boliviano
I misunderstood your comment - loading from the new file path fixed the problem. I didn't need the basePath part though, I posted the working code aboveBoliviano
H
3

the method writeToFile:atomically returns a BOOL result. I guess the file is not being saved at all.

The reason why your game works correctly is because the application reads the data from Dictionary (which is populated on launch once), not directly from plist file every time (and it shouldn't either).

Check to see the return value of writeToFile:atomically method to determine whether file is actually being saved or not.

Houlberg answered 5/9, 2012 at 4:49 Comment(2)
It returns NO. Is there another way to write to the file?Boliviano
#2010692Houlberg

© 2022 - 2024 — McMap. All rights reserved.