开发者

NSMutableArray/NSMutable Dictionary not saving data on writeToFile

I have a method that is called addHighScore. When a user wants to quit the game, they can save their score out. In the resources folder, I created a highScore.plist and populated it with one entry. The structure is:

   item 1 (array)
      Name (dictionary, string)
      Level (dictionary, string)
      Score(dictionary, Number)

My problem is this: When I run this in the simulator, after I load the arrHighScores and then add the newScore dictionary, everything seems fine and the records get added (and shown via the NSLog statement) but this is ONLY as long as the app is running. Once I quit, go back in, the only entry that exists is the one I manually entered.

When I run this on the Device (iPhone), it never shows the records added, even while still in the game. I have looked at just about every example I can concerning NSDictionary and can't seem to figure out what is going wrong.

Any Ideas or suggestions are greatly appreciated. Thanks in advance for any and all help. (Geo...)

My method looks like this:

 -(IBAction) addHighScore {
    NSString *myPath = [[NSBundle mainBundle] pathForResource:@"highScores" ofType:@"plist"];
    NSLog(@"myPath: %@", myPath);
    NSMutableArray *arrHighScores = [[NSMutableArray alloc] initWithContentsOfFile:myPath];

    NSMutableDictionary *newScore = [[NSMutableDictionary alloc] init];
    [newScore setValue:@"Geo" forKey:@"Name"];
    [newScore setValue:lblLevel.text forKey:@"Level"];
    [newScore setValue:[NSNumber numberWithDouble: dblScore] forKey:@"Score"];

    [arrHighScores addObject:newScore];

    for (int i = 0; i < [arrHighScores count]; i++) {
        NSLog(@"Retreiving (%d) --> %@", i, [arrHighScores objectAtIndex:i]);
    }

    [arrHighScores writeToFile:myPath atomically:YES];

    NSMutableArray *tmpArray2 = [[NSMutable开发者_如何学PythonArray alloc] initWithContentsOfFile:myPath];

    NSSortDescriptor *mySorter = [[NSSortDescriptor alloc] initWithKey:@"Score" ascending:YES];
    [tmpArray2 sortUsingDescriptors:[NSArray arrayWithObject:mySorter]];    

    for (int i = 0; i < [tmpArray2 count]; i++) {
        NSLog(@"Retreiving (%d) --> %@", i, [tmpArray2 objectAtIndex:i]);
    }

    [arrHighScores release];
    [tmpArray2 release];
    [mySorter release];
    [newScore release]; 

}


The problem is that you are attempting to save your changes in application bundle but you don't have permissions to write there. The correct place to save data is Documents directory in application sandbox - you can get it path:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                             NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0];

So the correct workflow should be:

  1. Try to load highscores data from Documents folder
  2. If file in Documents folder does not exist load data from resources
  3. Update data
  4. Save data to Documents folder

See also "Commonly Used Directories" for more information about where you can and should save you application data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜