0

我正在从我的应用程序的旧版本 (v2) 迁移到新版本 (v3),然后将每个玩家的两个新字段添加到 playerdata.plist 中。我将“99”放在新字段中的原因只是为了测试,因为我正在测试带有额外字段的 v3 文件,以使其更容易。

我想要完成的是:

一个。读取带有播放器数据的文件

湾。添加到新字段

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 回答