0

私の 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];
}
4

2 に答える 2

1

ダニエルが言ったように、NSArrayまたはNSDictionaryで、最初に古い値をロードする必要があります。

コレクションに新しい値を追加します。(多分いくつかのソートか何かもします)

次に、新しいコレクションをディスクに書き戻します。

于 2009-07-29T17:04:56.853 に答える
1

キースコアの数値にオブジェクトを設定しています

    NSNumber *number=[NSNumber numberWithFloat:userScore];   
 [dictionary setObject:number forKey:@"score"];

これの代わりに、あなたがしたいのは、配列またはそのようなものを持っていることです

NSNumber *number=[NSNumber numberWithFloat:userScore];  
    NSMutableArray *array=[dictionary objectForKey:@"score"]
     [array addObject:number]
    [dictionary setObject:array forKey:@"score"]

これはあなたが求めていることをするはずです

于 2009-07-29T16:59:11.750 に答える