How to append data to NSKeyedArchive?

风流意气都作罢 提交于 2019-12-11 08:39:15

问题


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?


回答1:


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.




回答2:


 NSString *archivePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"any name"];
[NSKeyedArchiver archiveRootObject:your object(example:singletonDataObjectref) toFile:archivePath];


来源:https://stackoverflow.com/questions/7663908/how-to-append-data-to-nskeyedarchive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!