2009-07-29 6 views
0

私のiPhoneアプリはplistファイルの辞書にキーと値のペアを書き込みます。私は基本的に彼らがゲームをプレイするときにユーザーのスコアを保存しています。これはすばらしくて素敵ですが、アプリを実行して新しいスコアを得るたびに、新しい値が古い値に保存されます。ファイルを書き換えるのではなく、ユーザーがアプリにアクセスするたびにplistに情報を追加するにはどうすればよいですか?私は最新のものだけでなく、すべてのスコアを保持したい。アプリを実行するたびにplistに追加情報を保存しますか? (iPhone)

コード:

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

答えて

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"] 

これはあなたに何を求めているのですか

+0

ああ。だからそれは配列です。本当にありがとう! – Evelyn

+0

私はまだ問題があります。私は辞書の配列を作成する必要があると言っていますか? – Evelyn

+0

いいえ、キーは数字の代わりに数字の配列にしてください、配列はオブジェクトのリストです... mのように任意の数字をudのように置くことができます...あなたはそれを可変にしたいです配列を追加して削除することができます。 – Daniel

1

ダニエル氏のようにNSArrayまたはNSDictionaryに古い値を先に読み込みたいとします。

新しい値をコレクションに追加しました。 (多分何らかのソートや何かをする)

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

+0

それも役立ちます。 :) – Evelyn

関連する問題