1

如何解决上述错误?

我使用以下代码使用 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];

行,我收到错误Automatic Reference Counting Issue: No known instance method for selector 'JSONValue'

我的项目使用 ARC。

如何解决该错误?

4

4 回答 4

3

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

#import <SBJson/SBJson.h>
于 2013-03-19T12:16:12.963 回答
2

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

于 2013-03-19T12:13:18.100 回答
1

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

执行以下步骤..

1)转到构建阶段->编译源。

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

3)清理项目并再次构建它。

此错误是由于 ARC 造成的。因为我们的 JSON 套件在您使用 ARC 时具有自动释放功能。无论如何,希望这对您有所帮助。祝您好运!

于 2013-03-19T12:28:40.117 回答
1

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

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

需要在 SBJson 库中找到的 NSString 类别。

不要忘记为其类禁用弧。

于 2013-03-19T12:49:57.343 回答