没有已知的选择器'JSONValue'的实例方法

时间:2013-03-19 12:09:37

标签: iphone ios objective-c json mkmapview

如何解决上述错误?

我使用以下代码使用google maps api

从地址字符串中获取latlong
- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address
{
  double latitude = 0.0;
  double longitude = 0.0;

  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"];   // get the results dictionary
  NSDictionary   *geometryDict = [resultsDict valueForKey: @"geometry"];   // geometry dictionary within the  results dictionary
  NSDictionary   *locationDict = [geometryDict valueForKey: @"location"];   // location dictionary within the geometry dictionary

  NSArray *latArray = [locationDict valueForKey: @"lat"];
   NSString *latString = [latArray lastObject];     // (one element) array entries provided by the json parser

  NSArray *lngArray = [locationDict valueForKey: @"lng"];
  NSString *lngString = [lngArray lastObject];     // (one element) array entries provided by the json parser

 CLLocationCoordinate2D location;
 location.latitude = [latString doubleValue];// latitude;
 location.longitude = [lngString doubleValue]; //longitude;

  return location;
}

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

行,我收到错误自动引用计数问题:选择器'JSONValue'没有已知的实例方法

我的项目使用ARC。

如何解决该错误?

4 个答案:

答案 0 :(得分:3)

JSONValue来自NSString之上添加的类别。要使用通过类别添加的方法,您需要包含相应的标题。在这种情况下,缺少的标题可能是

#import <SBJson/SBJson.h>

答案 1 :(得分:2)

NSString没有名为JSONValue的方法(为什么你认为它呢?)。也许您应该使用NSJSONSerialization类将字符串解析为NSDictionary和/或NSArray数据结构。

答案 2 :(得分:1)

我已经解决了这个问题...

执行以下步骤..

1)转到Build阶段 - &gt;编译源代码。

2)为与JSON相关的所有文件设置标志-fno-objc-arc。

3)清理项目并重新构建。

由于ARC发生此错误。我们的JSON套件在您使用ARC时具有自动释放功能。 无论如何,希望这可以帮助你.. 祝好运 !!

答案 3 :(得分:1)

看起来你需要臭名昭着的SBJson图书馆。

[[NSString stringWithContentsOfURL: [NSURL URLWithString: req] encoding: NSUTF8StringEncoding error: NULL] JSONValue];

需要一个NSString类别,可在SBJson Library中找到。

别忘了为其类禁用arc。