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?
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
}
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With