开发者

Is it possible to store multiple objects using object archving?

开发者 https://www.devze.com 2022-12-21 23:30 出处:网络
Okay, of course it is possible, that\'s no issue.In my code I have multiple objects that I work with, and they need to be data persistent.To do that I\'ve used object encoding and storing them in a da

Okay, of course it is possible, that's no issue. In my code I have multiple objects that I work with, and they need to be data persistent. To do that I've used object encoding and storing them in a data file. Such as:

-(NSString *)dataFilePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return [documentsDirectory stringByAppendingPathComponent:kFilename];
}

And then further along in another section of code:

NSMutableData *data = [[NSMutableData alloc] init];
        NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
        NSLog(@"Test 4");
        [archiver encodeObject:playerTwo forKey:@"PlayerTwoObject"];
        [archiver encodeObject:singlePlayer forKey:@"SinglePlayer"];
        [archiver encodeObject:playerOne forKey:@"PlayerOneObject"];
        [archiver encodeObject:settings forKey:@"Settings"];


        [archiver finishEncoding];
        [data writeToFile:[self dataFilePath] atomically:YES];
        [singlePlayer release];
        [playerOne release];
        [playerTwo release];
        [settings release]; 

And that saves the objects. I know it works, because I can take them out with this code:

unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:[[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]]];
    SettingsObject *settings = [unarchiver decodeObjectForKey:@"Settings"];
    Player *test = [unarchiver decodeObjectForKey:@"PlayerOneObject"];
    NSLog(@"Power On Settings Test: %d,%d", [settings playerOneStartingLife], [settings singlePlayerStartingLife]);
    NSLog(@"Power On Player Test: %@,%d", [test name], [test life]);

Cool! So now I can store my objects, and read them out. So this is where the issue comes in. In a different class I take out one of the objects I'm storing and work with it. Not all of them, I only read out one. And then I save it again after I change the settings. But I only save it:

NSMutableData *data = [[NSMutableData alloc] init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithM开发者_运维知识库utableData:data];
    [archiver encodeObject:singlePlayer forKey:@"SinglePlayer"];
    [archiver finishEncoding];
    [data writeToFile:[self dataFilePath] atomically:YES];
    [singlePlayer release];

After I have done that, all the rest of the objects I have stored are removed. Now I'm confused, because I thought I was only editing the one key "SinglePlayer", but apparently that somehow removes the rest of them? Is that supossed to happen? I have had some issues with releasing the archiver, would that affect it at all... I don't think it would. Does anyone have some insight on this, because I don't want to have to take all of the objects out and re-encode them if I only want to edit one of them...

Thanks for your help!


You create a new archiver, with an emtpy data object. You write the SinglePlayer object in the data, but you do not store the rest of the data in the same, newly created archive.

To do so, unarchive the whole previously stored archive, then adjust the SinglePlayer object and archive and save it again.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号