NSKeyedUnarchiver incomprehensible archive

I have a weird crash that happens to barley ever, and I wonder if this could be due to corrupted data? I have this error:

-[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 happened once in my application, but I'm just wondering if corrupted data is a viable cause for this. and if so, is there a way to deal with damaged data?

+4
source share
2 answers

You can wrap some of the code in @try @catch to evaluate the exception and avoid the crash. Here is an 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
}
+4
source

? , json . NSJSONSerialization.

:

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

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

       }
+3

Source: https://habr.com/ru/post/1530243/


All Articles