我无法初始化我在整个程序中用来存储用户成就和分数的词典。 我有两个字典几乎完全相同的代码,只有gameCenterData字典似乎正常工作。我试过改变plist文件名和内容但似乎没有任何东西让playerData字典正确加载文件中的信息,因为它应该
在根视图控制器中,我有以下代码(playerData和gameCenterData都是NSMutableDictionaries并且plist文件位于适当的位置)
-(NSString *)scoreFilePath
{
NSArray *scorePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [scorePath objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:@"PlayerScoreData.plist"];
}
-(NSString *)gameCenterFilePath
{
NSArray *gameCenterPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [gameCenterPath objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:@"GameCenterData.plist"];
}
然后视图加载了
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *playerDataPath = [self scoreFilePath];
if (! [[NSFileManager defaultManager] fileExistsAtPath:playerDataPath])
{
playerData = [NSMutableDictionary dictionaryWithContentsOfFile:[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"scoreData.plist"]];
[playerData writeToFile:[self scoreFilePath] atomically:YES];
NSLog(@"Player data file does not exist");
}
else
{
playerData = [[NSMutableDictionary alloc] initWithContentsOfFile:[self scoreFilePath]];
NSLog(@"player data file exists");
}
NSLog(@"scoreData is %@",playerData);
NSString *gameCenterPath = [self gameCenterFilePath];
if (! [[NSFileManager defaultManager] fileExistsAtPath:gameCenterPath])
{
gameCenterData = [NSMutableDictionary dictionaryWithContentsOfFile:[[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"gameCenterData.plist"]];
[gameCenterData writeToFile:[self gameCenterFilePath] atomically:YES];
NSLog(@"game center data file does not exist");
}
else
{
gameCenterData = [[NSMutableDictionary alloc] initWithContentsOfFile:[self gameCenterFilePath]];
NSLog(@"game center data file exists");
}
NSLog(@"gameCenterData is %@",gameCenterData);
输出如下
2012-08-05 11:46:49.991 GlobeRoller[6410:1be03] Player data file does not exist
2012-08-05 11:46:49.992 GlobeRoller[6410:1be03] playerData is (null)
2012-08-05 11:46:50.061 GlobeRoller[6410:1be03] game center data file does not exist
2012-08-05 11:46:50.062 GlobeRoller[6410:1be03] gameCenterData is {
"Career Odometer" = 0;
"Career Score" = 0;
"Cities Found" = 0;
"Offline Games Played" = 0;
"Online Games Played" = 0;
"Online Games Won" = 0;
}
我搜索了所有问题和答案,看看我是否能找出为什么这两种方法都不起作用。您可以提供的任何帮助,或者您可以指向我的资源,我将非常感激。
谢谢你, CF
答案 0 :(得分:1)
iOS区分大小写。你确定你的文件包中的文件是小写的,即“@”scoreData.plist“,而不是你的代码使用的名称大写吗?另外,验证这两个文件是否在你的包中 - 检查构建阶段或选择文件(一次一个)并查看文件属性部分的第3个Xcode窗格(以验证它们是否包含在目标中)。如果所有看起来都很好,那么当您尝试从捆绑中检索文件时:< / p>
此外,不要尝试在捆绑包的根级别找到该文件 - 您应该使用:
NSString *path = [[NSBundle mainBundle] pathForResource:@"GameCenterData" ofType:@"plist"];
NSLog(@"PATH is %@", path);
...then use path instead of the code you are using now
答案 1 :(得分:1)
您尝试从捆绑包加载的plist文件要么不存在,要么创建不正确。直接来自dictionaryWithContentsOfFile的文档:。
返回值
包含路径字典的新字典,或 如果存在文件错误或文件内容是否为nil,则为nil 字典的无效表示。
您应该确保使用正确的文件名,然后在Xcode中打开您的plist以查看它是否格式正确。