0

私はiPhoneを初めて使用します

GameData.plistファイルを保存し、「root」ディクショナリの下の「root」ディクショナリの下に「gameProgress」という名前の配列があり、この「gameProgress」配列にデータを挿入したいと思います。

以下は私のコードです

-(void)writeDataToPhone
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"storedGameData.plist"];

    // read or create plist

    NSMutableDictionary *dict;

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if ( [fileManager fileExistsAtPath:plistPath] ) {

        // Reading the Plist from inside the if

        //statement

        NSLog(@"dictionary existed, reading %@", plistPath);

        dict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];

    }

   NSNumber *newNumberOfCompletedLevels = [NSNumber numberWithInt:1];

    [dict setObject:newNumberOfCompletedLevels forKey:@"gameProgress"];

    NSLog(@"writing to %@…", plistPath);

    [dict writeToFile:plistPath atomically:YES];


NSLog(@"dictionary values are…:");

for (id key in dict) {

    NSLog(@"key=%@, value=%@", key, [dict objectForKey:key]);

    }
}

しかし、私の配列は空白です何も挿入されていません

私のコードの問題は何ですか?配列に値を挿入するには、コードで何を変更する必要がありますか

4

3 に答える 3

1

絶対にうまくいきます。私はただ、

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"storedGameData.plist"];

// read or create plist

NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];

NSFileManager *fileManager = [NSFileManager defaultManager];

if ( [fileManager fileExistsAtPath:plistPath] ) {

    // Reading the Plist from inside the if

    //statement

    NSLog(@"dictionary existed, reading %@", plistPath);

    dict = [NSMutableDictionary dictionary
}

NSNumber *newNumberOfCompletedLevels = [NSNumber numberWithInt:5];

[dict setObject:newNumberOfCompletedLevels forKey:@"gameProgress"];

NSLog(@"writing to %@…", plistPath);

[dict writeToFile:plistPath atomically:YES];


NSLog(@"dictionary values are…:");

for (id key in dict)
{

    NSLog(@"key=%@, value=%@", key, [dict objectForKey:key]);

}
于 2012-12-06T12:45:54.267 に答える
1

エラーはdictオブジェクトにあります。初期化していません。

次のコード行を使用してください。

NSMutableDictionary *dict = [NSMutableDictionary alloc] init];

それ以外の:

NSMutableDictionary *dict; 

それはあなたの問題を解決します。フィードバックをお寄せください。

于 2012-12-06T12:07:52.013 に答える