How to append data to NSKeyedArchive?

2019-07-31 08:32发布

I'm reading a file like so:

NSData *data = [NSData dataWithContentsOfFile:myPath];

NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];


NSData *imgData = [unarchiver decodeObjectForKey:@"myimage"];

if (imgData != nil) {
    self.image = [[NSImage alloc]initWithData:imgData];
}else{
    self.image = nil;
}

[unarchiver finishDecoding];
[unarchiver release];

What I'd like to do is to append image data for key @"myimage" to the same file if it doesn't exist.

How to go about it?

2条回答
贼婆χ
2楼-- · 2019-07-31 09:06

It seems that there's no way to "append" to NSKeyedArchive, so I just re-created it with the extra data added and have written over the original file.

查看更多
狗以群分
3楼-- · 2019-07-31 09:16
 NSString *archivePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"any name"];
[NSKeyedArchiver archiveRootObject:your object(example:singletonDataObjectref) toFile:archivePath];
查看更多
登录 后发表回答