1

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?

anna
  • 2,723
  • 4
  • 28
  • 37

2 Answers2

2

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.

anna
  • 2,723
  • 4
  • 28
  • 37
  • Yep. No methods provided for appending. If the data is large enough it may be worth using a good ol' unix FILE write/fwrite -- or using a DB. – wcochran Jul 22 '15 at 20:56
-1
 NSString *archivePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"any name"];
[NSKeyedArchiver archiveRootObject:your object(example:singletonDataObjectref) toFile:archivePath];
Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
dhaya
  • 1,522
  • 13
  • 21
  • Quite apart that it would be good form to add some comment explaining your code, there is the minor detail that it *does not* address the question asked by the OP. – Marco Massenzio Feb 24 '16 at 06:37