0

アプリの古いバージョン(v2)から新しいバージョン(v3)に移行し、それによって各プレーヤーの2つの新しいフィールドをplayerdata.plistに追加します。新しいフィールドに「99」を入力する理由は、追加のフィールドを使用してv3ファイルでテストしているため、テストを容易にするためだけです。

私が達成しようとしていることは次のとおりです。

a。プレーヤーデータを含むファイルを読み取る

b。新しいフィールドに追加

c。ファイルを保存する

現在の結果は、移行後に各プレーヤーに同じデータを保存していることです。

コード:

- (void)migratePlayerDataPlist {
//====THIS PIECE OF CODE SHOULD BE REMOVED AFTER CERTAIN TIME AS IT WILL NOT BE NEEDED====//
//====..WHEN ALL OLD USERS HAVE MIGRATED. THIS IS WRITTEN SEPTEMBER 5 2012 VERSION 3.0====//

// This procedure migrates the old playerdatafile to version 3 of the game

//Prepare File Manager
NSString *filePath = [self dataFilePath]; 

NSFileManager *fileMgr;
fileMgr = [NSFileManager defaultManager];

if ([fileMgr fileExistsAtPath: filePath] == YES) {


    dataArray = [[NSMutableArray alloc]init];

    NSMutableDictionary *gameFileDict = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];

    NSLog(@"gameFileDict: %@", gameFileDict);

    myKeyArray = [gameFileDict allKeys];

    int nrOfKeys = [myKeyArray count];

    NSMutableDictionary *migrationDict = [[NSMutableDictionary alloc]initWithCapacity:200];
    NSMutableDictionary *xmigrationDict = [[NSMutableDictionary alloc]initWithCapacity:200];
    NSLog(@"nrOfKeys: %i", nrOfKeys);

    for (int oo = 0; oo < nrOfKeys; oo++) {

        [dataArray removeAllObjects]; // Clean array

        theObjects = [gameFileDict valueForKey:[myKeyArray objectAtIndex:oo]];
        [dataArray addObject:[theObjects objectAtIndex:0]]; // diffSelection
        [dataArray addObject:[theObjects objectAtIndex:1]]; // # of games played
        [dataArray addObject:[theObjects objectAtIndex:2]]; // # correct answers
        [dataArray addObject:[theObjects objectAtIndex:3]]; // # questions
        [dataArray addObject:[NSNumber numberWithInt:99]];   // >>New field<< # of games won
        [dataArray addObject:[NSNumber numberWithInt:99]];   // >>New field<< # of games lost

        xmigrationDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:dataArray, [myKeyArray objectAtIndex:oo], nil];

        [migrationDict addEntriesFromDictionary:xmigrationDict];
    }

    NSLog(@"xmigrationDict: %@", xmigrationDict);
    NSLog(@"migrationDict: %@", migrationDict);
    NSLog(@"theObjects: %@", theObjects);

    // Delete the old file
    [fileMgr removeItemAtPath:filePath error:NULL];

    // Resave the file with the new data
    [migrationDict writeToFile:filePath atomically: TRUE];

    //  ...and the migration is done

}   
}

現在のNSLogベースの出力:

2012-09-06 20:57:50.332 xxxx_3[1505:fb03] gameFileDict: {
Barnspelare =     (
    1,
    1,
    2,
    2,
    1,
    0
);
"Ton\U00e5rsspelare" =     (
    2,
    0,
    0,
    0,
    0,
    0
);
Vuxenspelare =     (
    3,
    1,
    0,
    2,
    0,
    1
);
}
2012-09-06 20:57:50.334 xxxx_3[1505:fb03] nrOfKeys: 3
2012-09-06 20:57:50.335 xxxx_3[1505:fb03] xmigrationDict: {
"Ton\U00e5rsspelare" =     (
    2,
    0,
    0,
    0,
    99,
    99
);
}
2012-09-06 20:57:50.336 xxxx_3[1505:fb03] migrationDict: {
Barnspelare =     (
    2,
    0,
    0,
    0,
    99,
    99
);
"Ton\U00e5rsspelare" =     (
    2,
    0,
    0,
    0,
    99,
    99
);
Vuxenspelare =     (
    2,
    0,
    0,
    0,
    99,
    99
);
}
2012-09-06 20:57:50.336 xxxx_3[1505:fb03] theObjects: (
2,
0,
0,
0,
0,
0
)
4

1 に答える 1

0

これにはもっと簡単なアプローチがあると思います。方法は次のとおりです。

(という辞書にデータをフェッチしd、ファイルの読み取り/書き込みの部分をスキップしたと仮定します...)

    NSMutableDictionary *md = [d mutableCopy];
    for (NSString *key in d)
    {
        NSMutableArray *values = [[md valueForKey:key] mutableCopy];
        [values addObject:[NSNumber numberWithInt:99]]; // 1st new field
        [values addObject:[NSNumber numberWithInt:99]]; // 2nd new field
        [md setObject:values forKey:key];
    }

そして今、あなたの新しい辞書(md)には以下が含まれています:

{
    Barnspelare =     (
        1,
        1,
        2,
        2,
        1,
        0,
        99,
        99
    );
    "Ton\U00e5rsspelare" =     (
        2,
        0,
        0,
        0,
        0,
        0,
        99,
        99
    );
    Vuxenspelare =     (
        3,
        1,
        0,
        2,
        0,
        1,
        99,
        99
    );
}
于 2012-09-06T21:12:12.883 に答える