How to catch exception in iOS?

前端 未结 1 1632
小蘑菇
小蘑菇 2021-01-29 05:03

In JSON parse, save the parsing data into NSDictionary, And read the key-value like:

NSString *verCode = [JSONDict objectForKey:@\"v\"];

相关标签:
1条回答
  • 2021-01-29 05:54

    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;
    
    0 讨论(0)
提交回复
热议问题