Exception not handled in JSONObjectWithData iOS?
Clash Royale CLAN TAG#URR8PPP
Exception not handled in JSONObjectWithData iOS?
I am getting random crash while parsing NSData into dictionary. I used following code.
-(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];
Sometimes i am getting "nil" (Fine in worse case) but sometimes my app crash(Exception not catched).
I am sure responseData is not nil, When crash happens "error" does not give me reason (error found as nil).
App crash at method "JSONObjectWithData" itself. How i can fix this or handle in exception?
You can't catch all errors, and you can't catch the error if
responseData
is nil if I remember correctly. There might be other error that you can't catch. Just check if responseData
is nil.– Larme
Aug 8 at 7:52
responseData
responseData
No point enclosing this in
@try @catch
since in Objective-C this method does not throw an exception, or at least not due to serialization error.– mag_zbc
Aug 8 at 7:53
@try @catch
@Larme. responseData is not nil. as i send message JSONObjectWithData to response data it gets crashed. any workaround ?
– Avijit Nagare
Aug 8 at 7:58
How do you get
responseData
? Does your response return NSData
, or does it return NSString
, that you turn to NSData
using dataUsingEncoding
?– mag_zbc
Aug 8 at 8:02
responseData
NSData
NSString
NSData
dataUsingEncoding
1 Answer
1
JSON Deserialization only Dictionary or Array, so you can check and see the log.
Other, see options type: -> Enumeration NSJSONReadingOptions like "options:NSJSONReadingAllowFragments"
typedef enum NSJSONReadingOptions : NSUInteger
NSJSONReadingMutableContainers = (1UL << 0),
NSJSONReadingMutableLeaves = (1UL << 1),
NSJSONReadingAllowFragments = (1UL << 2)
NSJSONReadingOptions;
Example:
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);
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
It seems it is unrelated to "Swift", is there a need to add its tag?
– Ahmad F
Aug 8 at 7:50