0

これは、viewDidLoad メソッドで plist の辞書にアクセスする方法です。

     NSString* documentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
     NSString *fileName = [NSString stringWithFormat:@"Level.plist"];
     filePath = [documentsDir stringByAppendingPathComponent:fileName];
     array = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
     dict = [array objectAtIndex:1];

これはうまくいきます。次に、次のように辞書に書き込みます。

    score = [NSString stringWithFormat:@"%d", score.integerValue + 10];
    [dict setObject:score forKey:@"Score"];
    [dict writeToFile:filePath atomically: NO];

これも問題なく動作しますが、このビューに戻って viewDidLoad メソッドでディクショナリに再度アクセスしようとすると、ディクショナリに対して (null) が返されます。

4

1 に答える 1

0

これは、ファイルを配列として読み取っているためですが、もう一度書き戻すと、その辞書を含む配列ではなく、辞書のみを書き出すことになります。NSMutableArray を使用して、ルートに配列以外のものがある plist ファイルを読み取ろうとすると、nil が返されます。したがって、コードの最後の行をdictwitharrayに置き換えるだけでうまくいくはずです。

于 2013-01-17T00:37:09.953 に答える