问题
In JSON parse, save the parsing data into NSDictionary,
And read the key-value
like:
NSString *verCode = [JSONDict objectForKey:@"v"];
but sometime, the key-value is other format, the program will crash, so I need covert to stringValue;
like this:
NSString *verCode = [[JSONDict objectForKey:@"v"] stringValue];
How can I catch exception in iOS program?
回答1:
You can use @try{ } around code to catch exceptions.
In your case though a better approach is to look at the types of the object in the array for a key and use the right conversion as needed:
id object = [myJSONDict objectForKey:@"theValue"];
NSString *finalVal = nil;
if ( [object isKindOfClass:[NSNumber class]] )
finalVal = [object stringValue]
else
finalVal = object;
来源:https://stackoverflow.com/questions/8584687/how-to-catch-exception-in-ios