0

我无法初始化我在整个程序中使用的字典来存储用户成就和分数。我有两个字典几乎相同的代码,只有 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

4

2 回答 2

1

您尝试从包中加载的 plist 文件不存在,或者创建不正确。直接来自dictionaryWithContentsOfFile: 的文档。

返回值

一个新字典,包含路径中的字典,如果存在文件错误或文件的内容是字典的无效表示,则返回 nil。

您应该确保使用正确的文件名,然后在 Xcode 中打开您的 plist 以查看其格式是否正确。

于 2012-08-05T13:13:44.730 回答
1

iOS 区分大小写。你确定你的包中的文件是小写的,即“@”scoreData.plist”,而不是像你的代码使用的名称那样大写吗?另外,验证这两个文件是否在你的包中 - 检查构建阶段或选择文件(一次一个)并查看文件属性部分的第三个 Xcode 窗格(以验证它们是否包含在您的目标中。如果一切看起来都不错,那么当您尝试从包中检索文件时:

此外,不要尝试在包的根级别查找文件 - 您应该使用:

NSString *path = [[NSBundle mainBundle] pathForResource:@"GameCenterData" ofType:@"plist"];
NSLog(@"PATH is %@", path);
...then use path instead of the code you are using now
于 2012-08-05T11:51:59.740 回答