我有一個名爲addHighScore的方法。當用戶想要退出遊戲時,他們可以保存得分。在資源文件夾中,我創建了一個highScore.plist並用一個條目填充它。其結構是:NSMutableArray/NSMutable字典不保存數據writeToFile
item 1 (array)
Name (dictionary, string)
Level (dictionary, string)
Score(dictionary, Number)
我的問題是這樣的:當我在模擬器中運行這個後,我加載arrHighScores然後添加newScore字典,一切似乎都很好,這些記錄會添加(並顯示通過的NSLog聲明),但只有在應用程序運行時纔有效。一旦我退出,回到原處,唯一存在的條目就是我手動輸入的條目。
當我在設備(iPhone)上運行它時,它從不顯示添加的記錄,即使仍在遊戲中。我已經看過幾乎所有關於NSDictionary的例子,並且似乎無法弄清楚發生了什麼問題。
任何意見或建議,非常感謝。預先感謝任何和所有幫助。 (GEO ...)
我的方法是這樣的:
-(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 = [[NSMutableArray 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];
}