gpt4 book ai didi

objective-c - 在 plist 中更新和保存数据

转载 作者:可可西里 更新时间:2023-11-01 04:16:29 27 4
gpt4 key购买 nike

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

在游戏的init中,获取玩家达到的最高等级(原值为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];
}

并在初始化

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

最佳答案

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

例如,从您的资源中加载一个 plist:

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

编写一个 plistdict 同样简单:

[plistdict writeToFile:filePath atomically:YES];

请注意您不能写入应用的资源,因此您必须创建不同的路径,例如在您的 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];

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

 NSMutableDictionary *plistdict = [NSMutableDictionary dictionaryWithContentsOfFile:docfilePath];

将你的内容添加到 plistDict 中并重新写入。

关于objective-c - 在 plist 中更新和保存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12274530/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com