私の iPhone アプリはキーと値のペアを plist ファイルのディクショナリに書き込みます。基本的に、ユーザーがゲームをプレイするときにスコアを保存しています。これは問題ありませんが、アプリを実行して新しいスコアを取得するたびに、新しい値が古い値の上に保存されます。ファイルを書き換えるのではなく、ユーザーがアプリにアクセスするたびに plist に情報を追加するにはどうすればよいですか? 最新のスコアだけでなく、すべてのスコアを保持したい。
コード:
-(void)recordValues:(id)sender {
//read "propertyList.plist" from application bundle
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path
stringByAppendingPathComponent:@"propertyList.plist"];
dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:finalPath];
//create an NSNumber object containing the
//float value userScore and add it as 'score' to the dictionary.
NSNumber *number=[NSNumber numberWithFloat:userScore];
[dictionary setObject:number forKey:@"score"];
//dump the contents of the dictionary to the console
for (id key in dictionary) {
NSLog(@"memory: key=%@, value=%@", key, [dictionary
objectForKey:key]);
}
//write xml representation of dictionary to a file
[dictionary writeToFile:@"/Users/rthomas/Sites/propertyList.plist" atomically:NO];
}