我正在开发一款需要使用 plist 的游戏。我正确地设置了 plist,或者至少我希望如此,因为我一直在使用这种方法来设置 plist。但问题是,在这种情况下,它甚至无法识别 plist。以下是我的 plist 的内容:
'yo' 键和值是调试值,我用它来查看 plist 是否被识别。这是我的代码:
-(NSString *)docsDir{
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
}
-(void)UpdatePlist{
listPath = [[self docsDir] stringByAppendingPathComponent:@"details.plist"];
self.savedData = [NSMutableDictionary dictionaryWithContentsOfFile:listPath];
unsigned long storedHighScore = [[self.savedData valueForKey:@"High Score"] unsignedLongValue];
NSLog(@"%@",[[self.savedData valueForKey:@"yo"] stringValue]);
NSLog(@"Here");
if (score>storedHighScore) {
[self.savedData setValue:[NSNumber numberWithUnsignedLong:score] forKey:@"High Score"];
NSLog(@"YO %lu",[[self.savedData valueForKey:@"High Score"] unsignedLongValue]);
}
[self.savedData writeToFile:listPath atomically:YES];
}
问题源于代码或我假设的 plist 声明。所有 NSLog 都按预期执行,但我的日志文件更有趣:
甚至测试字符串也不会返回并被视为空值。此时我假设 plist 有问题,是的,我可以向您保证 plist 的名称确实是 details.plist。
最佳答案
这个 plist 在哪里?它最初何时/如何写入文档目录?
文档目录是从 iTunes 复制文件的目录。因此,如果您将此文件放入应用的资源中,它不会以文档形式结束。
当 plist 不存在时,-dictionaryWithContentsOfFile:
将返回 nil
。在 nil
上调用 -objectForKey:
或 -stringValue
再次返回 nil
。因此,如果文件不存在,self.savedData
将被设置为 nil
。在 nil
上调用 -writeToFile:atomically:
也是空操作。因此,如果 plist 不存在,您发布的代码将永远不会创建它。
您是否尝试过在调试器中逐步执行此代码?单击 -dictionaryWithContentsOfFile:
行左侧的间距,查看 self.savedData
设置为什么?
关于ios - SpriteKit .plist 问题 xcode 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26622869/