2013-03-19 27 views
1

如何解決上述錯誤?沒有已知的選擇器實例方法'JSONValue'

我用下面的代碼中使用谷歌地圖API

- (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。

如何解決這個錯誤?

+0

你有沒有提到「XXXJSON.h」? – 2013-03-19 12:12:57

+0

請參閱http://stackoverflow.com/questions/9762432/get-values-from-json-string-objective-c – Bigood 2013-03-19 12:17:11

回答

3

JSONValue來自在NSString之上添加的類別。爲了使用通過類別添加的方法,您需要包含相應的標題。在這種情況下,缺少的頭可能是

#import <SBJson/SBJson.h> 
+0

你當然也需要在你的版本中有相應的.m文件。 – 2013-03-19 12:20:16

2

NSString沒有名爲JSONValue的方法(您爲什麼假設它的確如此?)。也許您應該使用NSJSONSerialization類將字符串解析爲NSDictionary和/或NSArray數據結構。

+0

無法理解,請說明我必須做什麼? – NAZIK 2013-03-19 12:15:04

+0

@NAZIK你必須閱讀文檔。 – 2013-03-19 12:15:21

+1

***請解釋downvote。***(這是**錯誤**,因爲這個答案在技術上是正確的。) – 2013-03-19 12:21:04

1

我已經經歷了這個問題了......

執行以下步驟..

1)去構建phases->編譯源代碼。

2)爲與JSON相關的所有文件設置標誌-fno-objc-arc。

3)清理項目並重新構建。

此錯誤是由於ARC ..因爲我們的JSON套件具有autorelease功能,而您正在使用ARC .. 不管怎樣,希望這可以幫助你.. 祝你好運!

1

看起來你需要臭名昭着的SBJson圖書館。

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

需要在SBJson庫中找到的NSString類別。

不要忘記禁用其類的弧。

相關問題