iphone - 如何解决 -[__NSCFString JSONValue] : unrecognized selector sent to instance when application terminates?

标签 iphone ios

大家好,我已经实现了如下所示的代码,但是当运行此代码时,它会终止并显示异常,如下所示如何处理异常以获取 iphone 中的纬度和经度

- (CLLocationCoordinate2D) geoCodeUsingAddress: (NSString *) address
{
    CLLocationCoordinate2D myLocation; 



    NSString *esc_addr = [address stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

    NSString *req = [NSString stringWithFormat: @"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr];

    NSDictionary *googleResponse = [[NSString stringWithContentsOfURL: [NSURL URLWithString: req] encoding: NSUTF8StringEncoding error: NULL] JSONValue];

    NSDictionary *resultsDict = [googleResponse valueForKey:  @"results"];
    NSDictionary *geometryDict = [resultsDict valueForKey: @"geometry"];     
    NSDictionary *locationDict = [geometryDict valueForKey: @"location"];       
    NSArray *latArray = [locationDict valueForKey: @"lat"]; 
    NSString *latString = [latArray lastObject];   
    NSArray *lngArray = [locationDict valueForKey: @"lng"];
    NSString *lngString = [lngArray lastObject];    

    myLocation.latitude = [latString doubleValue];     
    myLocation.longitude = [lngString doubleValue];

    NSLog(@"lat: %f\tlon:%f", myLocation.latitude, myLocation.longitude);
    return myLocation;
}

来自控制台的错误:

[1055:11603] -[__NSCFString JSONValue]: unrecognized selector sent to instance 0xabcc200
[1055:11603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString JSONValue]: unrecognized selector sent to instance 0xabcc200'
*** First throw call stack:
(0x16ee052 0x13d9d0a 0x16efced 0x1654f00 0x1654ce2 0x25a5 0x248f 0x16efec9 0x3945c2 0x39455a 0x439b76 0x43a03f 0x4392fe 0x3b9a30 0x3b9c56 0x3a0384 0x393aa9 0x1de4fa9 0x16c21c5 0x1627022 0x162590a 0x1624db4 0x1624ccb 0x1de3879 0x1de393e 0x391a9b 0x1dcd 0x1d45)
terminate called throwing an exception(lldb) 

最佳答案

您可能忘记包含 json 库,并且缺少 NSString JSONValue 类别。

关于iphone - 如何解决 -[__NSCFString JSONValue] : unrecognized selector sent to instance when application terminates?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9938463/

相关文章:

ios - 如何在 phonegap 3.0 中阻止 UIWebView 垂直弹跳

javascript - 如果安装了Cydia,有办法显示文本吗?

ios - 我将如何从一系列不同的单词或句子中淡入和淡出?

iphone - 检测 UITableView 中的触摸和按住

iphone - 在 Mac 中使用终端应用程序将 .p12 文件转换为 .pem "No such file or directory"错误?

ios - 蒙面的 UIVisualEffectView 在 iOS 10 上不起作用

ios - 使应用程序在锁屏时显示为 iOS 8 建议的应用程序

iphone - 设置委托(delegate)时 ASIHTTPRequest 崩溃?

iphone - 如何在Appstore上提交iphone申请,有什么前置要求?

ios - 如何从同一个类中的 prepareForSegue 方法访问覆盖 func tableView 属性