如何将数据追加到NSKeyedArchive?(How to append data to NSKe

2019-09-28 19:02发布

我读像这样一个文件:

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];

我想要做的就是追加图像数据的关键@“MYIMAGE”到同一个文件,如果它不存在。

如何做呢?

Answer 1:

看来,有没有办法“追加”到NSKeyedArchive,所以添加了额外的数据我只是重新创建它,并写在原始文件。



Answer 2:

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


文章来源: How to append data to NSKeyedArchive?