在plist中更新和保存数据

时间:2012-09-05 04:33:16

标签: objective-c ios plist

我正在制作iOS游戏,需要保存玩家达到的最高等级。我可以成功更改plist中的数据元素,但由于某种原因,每次游戏重新启动时,该数据都会恢复为原始值。以下是我的代码的基本流程:

在游戏的初始化中,获得玩家达到的最高等级(原始值为1)

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

'data'是一个NSMutableDictionary,它在PlayerData中被初始化为:

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

稍后,如果玩家击败最高级别,我会增加“最高级别”值并通过在PlayerData中调用此函数来写入该文件:

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

我知道这一切都有效,因为我有一个玩家在玩游戏时可以访问的关卡菜单。每次玩家按下关卡菜单按钮时,都会创建一个UITableView的子类,显示级别1到达达到的最高级别。初始化如下所示:

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

级别菜单在游戏中显示正确的等级数(例如,如果玩家没有击败任何等级并进入等级菜单,则只显示“等级1”;但如果玩家击败等级1并且转到级别菜单,它显示“级别1”和“级别2”)。但是,每当应用程序被终止或用户退出游戏主菜单时,“最高级别”的值将恢复为1并且此代码:

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

在玩家按下开始时始终将currentLevel设置为1,无论用户在玩游戏时有多少级别。

为什么价值会改变?我错过了一些会让我的plist编辑永久性的东西吗?

编辑:

这是我新的'newHighestLevel'方法:

-(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设置为YES。如果我将'docfilePath'更改为@“playerdata.plist”,则将其设置为NO。在任何一种情况下,游戏似乎都没有改变。

解决方案:

-(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];
}

并在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;
}

2 个答案:

答案 0 :(得分:11)

从plist创建字典的最简单方法是使用方法 dictionaryWithContentsOfFile:

例如,要从资源加载plist:

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

写一个plistdict同样简单:

[plistdict writeToFile:filePath atomically:YES];

注意您无法写入应用的资源,因此您必须创建不同的路径,例如在plist的Documents目录中。

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];

现在再次从plist中检索数据。

 NSMutableDictionary *plistdict = [NSMutableDictionary dictionaryWithContentsOfFile:docfilePath];

在plistDict中添加您的内容并重新编写。

答案 1 :(得分:2)

方法writeToFile:atomically会返回BOOL个结果。我想文件根本没有保存。

您的游戏正常运行的原因是因为应用程序从Dictionary(在启动时填充)中读取数据,而不是每次都直接来自plist文件(并且它不应该)。

检查writeToFile:atomically方法的返回值,以确定文件是否实际保存。