Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSKeyedUnarchiver incomprehensible archive

I'm having a weird crash that happens barley ever and i'm wondering if it could be due to corrupt data being read? I have this error occuring:

-[NSKeyedUnarchiver initForReadingWithData:]: incomprehensible archive


> # Binary Image Name   Address Symbol 0    CoreFoundation  0x3357b2a3  __exceptionPreprocess
> 1 libobjc.A.dylib 0x3b3df97f  objc_exception_throw
> 2 CoreFoundation  0x3357b1c5  -[NSException initWithCoder:]
> 3 Foundation  0x33e124ef  -[NSKeyedUnarchiver initForReadingWithData:]
> 4 Foundation  0x33e73537  +[NSKeyedUnarchiver unarchiveObjectWithFile:]

My code is fine and this has happened once in my app but i'm just wondering if corrupt data is a viable reason for this to occur. and if so is there a way to deal with corrupt data?

like image 592
user1179321 Avatar asked Mar 21 '23 08:03

user1179321


2 Answers

You can wrap you part of code in @try @catch construction to evaluate exception and avoid a crash. Here is example:

- (UIImage*) loadImageFromCacheWithFilePath: (NSString*) somePath {

    UIImage* image = nil;

    @try {
        image = [NSKeyedUnarchiver unarchiveObjectWithFile:somePath];
    } @catch (NSException* exception) {
        // Surpress any unarchiving exceptions and continue with nil
        NSLog(@"Load image from cache was failed with exception: %@", [exception reason]);
    }

    return image; //This will return nil if exception caught
}
like image 80
fir Avatar answered Apr 02 '23 04:04

fir


are you still experiencing this problem? It seems like you are trying to unarchive a json file. Try to unarchive your data with NSJSONSerialization.

Here is a little sample code:

 NSError * serializationError = nil;
 NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&serializationError];

       if(serializationError == nil && jsonArray != nil) {                            
           NSLog(@"%@", jsonArray);

       }
like image 33
dirtydanee Avatar answered Apr 02 '23 04:04

dirtydanee