Cba*_*bas 5 objective-c plist ios
我正在制作一款iOS游戏,需要保存玩家所达到的最高等级.我可以成功更改plist中的数据元素,但由于某种原因,每次游戏重新启动时,该数据都会恢复为原始值.这是我的代码的基本流程:
在游戏的初始化中,获得玩家达到的最高等级(原始值为1)
pData = [[PlayerData alloc] init];
currentLevel = [[pData.data valueForKey:@"Highest Level"] intValue];
[self startNewLevel:currentLevel];
Run Code Online (Sandbox Code Playgroud)
'data'是一个NSMutableDictionary,它在PlayerData中被初始化为:
self.data = [NSMutableDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"playerdata" ofType:@"plist"]];
Run Code Online (Sandbox Code Playgroud)
之后,如果玩家击败最高级别,我会增加"最高级别"值并通过在PlayerData中调用此函数来写入该文件:
-(void) newHighestLevel:(NSString*)path :(int)level{
[self.data setValue:[NSNumber numberWithInt:level] forKey:path];
[self.data writeToFile:@"playerdata.plist" atomically:YES]
Run Code Online (Sandbox Code Playgroud)
我知道这一切都有效,因为我有一个玩家在玩游戏时可以访问的关卡菜单.每次玩家按下关卡菜单按钮时,都会创建一个UITableView的子类,显示级别1到达达到的最高级别.初始化如下所示:
levelMenu = [[LevelMenu alloc] init:[[pData.data valueForKey:@"Highest Level"] intValue]];
Run Code Online (Sandbox Code Playgroud)
水平菜单在游戏中显示正确的等级数(例如,如果玩家没有击败任何等级并进入等级菜单,则只显示"等级1";但如果玩家击败等级1并进入级别菜单,显示"级别1"和"级别2").但是,每当应用程序终止或用户退出游戏主菜单时,"最高级别"的值将恢复为1并且此代码:
currentLevel = [[pData.data valueForKey:@"Highest Level"] intValue];
Run Code Online (Sandbox Code Playgroud)
无论用户在比赛中击败多少级别,总是在玩家按下开始时将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];
}
Run Code Online (Sandbox Code Playgroud)
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];
}
Run Code Online (Sandbox Code Playgroud)
并在初始化
-(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;
}
Run Code Online (Sandbox Code Playgroud)
Par*_*iya 11
从plist创建字典的最简单方法是使用方法dictionaryWithContentsOfFile : ,
例如,要从资源加载plist:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"playerData" ofType:@"plist"];
NSMutableDictionary *plistdict = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
Run Code Online (Sandbox Code Playgroud)
写一个plistdict同样简单:
[plistdict writeToFile:filePath atomically:YES];
Run Code Online (Sandbox Code Playgroud)
请注意,您无法写入应用程序的资源,因此您必须创建不同的路径,例如在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];
Run Code Online (Sandbox Code Playgroud)
现在再次从plist中检索数据.
NSMutableDictionary *plistdict = [NSMutableDictionary dictionaryWithContentsOfFile:docfilePath];
Run Code Online (Sandbox Code Playgroud)
在plistDict中添加您的内容并再次写入.
| 归档时间: |
|
| 查看次数: |
16058 次 |
| 最近记录: |