我在将NSData
解析为字典时遇到了随机崩溃。我使用了以下代码。
-(NSArray *)enumDataParser:(NSMutableData *)responseData
{
@try {
NSError *error;
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
}
@catch (NSException *exception) {
[[IFieldServiceCrashLog sharedLog] writeExceptionLogFile:exception];
[self performCatchOperation:exception];
}
}
有时我得到“零”,有时我的应用程序崩溃(Exception not catched
)。
我相信responseData
不是零,因为每当碰撞发生时,"error"
都没有给我一个理由(错误发现为零)。
应用程序在方法JSONObjectWithData
本身崩溃。如何在异常中修复此问题或处理?
JSON反序列化只有字典或数组,因此您可以检查并查看日志。其他,请参阅选项类型: - > Enumeration NSJSONReadingOptions,例如“options:NSJSONReadingAllowFragments”
typedef enum NSJSONReadingOptions : NSUInteger {
NSJSONReadingMutableContainers = (1UL << 0),
NSJSONReadingMutableLeaves = (1UL << 1),
NSJSONReadingAllowFragments = (1UL << 2)
} NSJSONReadingOptions;
例:
NSError *error = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
if (error != nil) {
NSLog(@"***** %@", error.localizedDescription);
} else if (jsonObject != nil) {
NSLog(@"*** JSON Data converted to NSObject!");
if ([jsonObject isKindOfClass:[NSDictionary class]]) {
NSDictionary *deserializedDictionary = (NSDictionary *)jsonObject;
NSLog(@"JSON Obj class: %@", NSStringFromClass([deserializedDictionary class]));
NSLog(@"*** its Dictionary Object from JSON Data: %@", deserializedDictionary.description);
} else if ([jsonObject isKindOfClass:[NSArray class]]) {
NSArray *deserializedArray = (NSArray *)jsonObject;
NSLog(@"JSON Obj class: %@", NSStringFromClass([deserializedArray class]));
NSLog(@"*** its Array Object from JSON Data: %@", deserializedArray.description);
} else {
NSLog(@"JSON Obj class: %@", NSStringFromClass([jsonObject class]));
NSLog(@"*** its UNKNOWN Object from JSON Data: %@", jsonObject);
}
}