当前位置: 代码迷 >> 综合 >> 使用[NSKeyedUnarchiver unarchiveObjectWithData:data]程序crash
  详细解决方案

使用[NSKeyedUnarchiver unarchiveObjectWithData:data]程序crash

热度:27   发布时间:2023-12-17 12:17:40.0

原文链接:

使用NSKeyedArchiver压缩对象成二进制数据,再使用NSKeyedUnarchiver解压二进制数据,如果解压时数据为nil,或者数据中有异常的数据,那么解压将会出错,甚至会导致程序crash掉。可以用这种方法防止程序crash:

 NSData *data=[[NSUserDefaults standardUserDefaultsvalueForKey:key];

    NSMutableDictionary *dict = [[NSMutableDictionary allocinit];

    if (data) {

        @try {

            dict=[NSKeyedUnarchiver unarchiveObjectWithData:data];

            return dict;

        }

        @catch (NSException *exception) {

            return dict;

        }

        @finally {

            

        }

    }

    return dict;


  相关解决方案