0

I usually encode my data in a NSFileWrapper like this (I leave out the NSFileWrapper bit):

-(NSData*)encodeObject:(id<NSCoding>)o {
        @autoreleasepool {            
            NSMutableData *data = [NSMutableData data];
            NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
            [archiver encodeObject:o forKey:@"data"];
            [archiver finishEncoding];
            return data;
        }
    }

And I usually get my data back when doing this:

- (id)decodeObjectFromWrapperWithPreferredFilename:(NSString *)p {

    NSFileWrapper *wrapper = [self.fileWrapper.fileWrappers objectForKey:p];
    if (!wrapper) {
        NSLog(@"Unexpected error: Couldn't find %@ in file wrapper!", p);
        return nil;
    }

    NSData *data = [wrapper regularFileContents];
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];

    NSLog(@"%@", [unarchiver decodeObjectForKey:@"data"]);
    return [unarchiver decodeObjectForKey:@"data"];

}

Sometimes, I get NSData back (it is not nil), but [unarchiver decodeObjectForKey:@"data"] will return NIL. It appears as if there is no object for the key @"data" even though there should be. I guess something must have gone wrong when encoding, but I'm not sure how to trouble shoot this. Can I just take whatever is in data and encode it, not worrying if it has got the right key? I mean there should only ever be one key "data".

n.evermind
  • 11,944
  • 19
  • 78
  • 122
  • 1
    Why is your code so complicated? Why not just `return [NSKeyedArchiver archivedDataWithRootObject:o];` and `return [NSKeyedUnarchiver unarchiveObjectWithData:data];` ? – deanWombourne Oct 01 '12 at 10:48
  • @deanWombourne Good question. I was so confused by NSFileWrapper that I followed Apple's Examples and Wenderlich's tutorial.... so I guess I made the mistake of blindly following convoluted code which I didn't understand 100%. I will give this a try right now and will let you know. Perhaps put your comment into an answer below? Thanks! – n.evermind Oct 01 '12 at 10:53
  • 1
    Because my comment doesn't actually _answer_ your question - there might have been a reason you needed that complexity. However, as there isn't, see my answer below :) – deanWombourne Oct 01 '12 at 10:59

1 Answers1

1

Why is your code so complicated :) The NSKeyedArchiver class has helper methods that will do what you want more simply :

// to turn an object into NSData
return [NSKeyedArchiver archivedDataWithRootObject:o]; 

// To turn NSData into your object again
return [NSKeyedUnarchiver unarchiveObjectWithData:data];
deanWombourne
  • 38,189
  • 13
  • 98
  • 110
  • Hmmm... my only problem is that return [NSKeyedUnarchiver unarchiveObjectWithData:data]; will not unarchive data with a certain key (it will now always return nil as my data was previously 'keyed' with my convoluted code). Is there anyway to extract an object without knowing its key? – n.evermind Oct 01 '12 at 11:08