0

I am having trouble initializing dictionaries I use throughout my program to store user achievements and scores. I have almost identical code for the two dictionaries and only the gameCenterData dictionary seems to be working properly. I have tried altering the plist file name and contents yet nothing seems to make the playerData dictionary properly load info from the file as it should

In the Root View Controller I have the following code (playerData and gameCenterData are both NSMutableDictionaries and the plist files are in the proper place)

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

then the view did load

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

the output is as follows

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

I have searched all of the questions and answers to see if I can find out why this isn't working for both methods. Any help you could offer, or resources you could point me to I would greatly appreciate.

Thank you, CF

Nairb555
  • 3
  • 2

2 Answers2

1

iOS is case sensitive. Are you sure that your file in the bundle is lower case, i.e. "@"scoreData.plist", and not upper case like the name your code uses? Also, verify that these two files are in your bundle - check the build phase or select the files (one at a time) and look in the 3rd Xcode pane in the file attribute section (to verify they are included in your target). If all that looks good then when you try to retrieve the files from your bundle:

Also, don't try to find the file at the root level of the bundle - you should be using:

NSString *path = [[NSBundle mainBundle] pathForResource:@"GameCenterData" ofType:@"plist"];
NSLog(@"PATH is %@", path);
...then use path instead of the code you are using now
David H
  • 40,852
  • 12
  • 92
  • 138
  • Thank you for your help, I have double checked and didn't see any capitalization errors in the meaningful text (the NSLog statements could be clearer but that would have no bearing on the performance). Are you finding capitalization errors in my instance variables or in the boilerplate methods i am using. I guess my question is why would one work and the other not? is there a limit to the size of plist? - the failing one is 75 objects and keys. Should I title it differently? with identical code I am just frustrated trying to track down a reason the first fi else statement won't work – Nairb555 Aug 05 '12 at 12:51
1

The plist file you are trying to load from the bundle is either not there, or has been created improperly. Directly from the documentation of dictionaryWithContentsOfFile:.

Return Value

A new dictionary that contains the dictionary at path, or nil if there is a file error or if the contents of the file are an invalid representation of a dictionary.

You should make sure you are using the proper file name, and then open your plist in Xcode to see if it is properly formatted.

Jody Hagins
  • 27,943
  • 6
  • 58
  • 87
  • Thank You! I had double checked that probably a dozen times since I wrote that code this morning and I knew I had the spelling correct. I never got a warning that the plist was corrupted so I never deleted it and tried making it again. I guess when I was editing in an XML editor I messed it up somehow. Thank you for your reply! – Nairb555 Aug 05 '12 at 13:18