This question already has answers here:
Getting warning in iPhone: NSString may not respond to '-JSONValue'

(3个答案)



No known instance method for selector

(3个答案)


7年前关闭。




如何解决以上错误?

我使用下面的代码使用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。

如何解决该错误?

最佳答案

JSONValue来自NSString顶部添加的类别。为了使用通过类别添加的方法,您需要包括相应的标题。在这种情况下,丢失的标题可能是

#import <SBJson/SBJson.h>

关于iphone - 没有已知的选择器'JSONValue'实例方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15499126/

10-11 07:19