问题
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