2011-02-17 137 views
1

有問題。這裏是我的代碼:stringByReplacingOccurrencesOfString不按預期方式工作

Latitude = [TBXML textForElement:lat]; //Latitude & Longitude are both NSStrings 
Longitude= [TBXML textForElement:lon]; 
NSLog(@"LAT:%@ LON:%@",Latitude,Longitude); 
NSString *defaultURL = @"http://api.wxbug.net/getLiveWeatherRSS.aspx?ACode=000000000&lat=+&long=-&unittype=1"; 
newURL = [[defaultURL stringByReplacingOccurrencesOfString:@"+" 
                 withString:Latitude] 
            stringByReplacingOccurrencesOfString:@"-" 
                 withString:Longitude]; 
NSLog(@"%@",newURL); 

而這裏的輸出:

LAT:-33.92 LON:18.42 
http://api.wxbug.net/getLiveWeatherRSS.aspxACode=000000000&lat=18.4233.92&long=18.42&unittype=1 

正如你可以看到,一些奇怪的事情正在發生的附加碼。我在這裏做錯了什麼?

回答

7

之前更換經度,該字符串是

http://....&lat=-33.92&long=-&... 
       ^  ^

系統發現有兩個-,因此他們兩人將緯度所取代。


您應該使用更具描述性的字符串來替換,例如,

NSString *defaultURL = @"http://....&lat={latitude}&long={longitude}&unittype=1"; 
newURL = [defaultURL stringByReplacingOccurrencesOfString:@"{latitude}" 
               withString:Latitude]; 
newURL = [newURL stringByReplacingOccurrencesOfString:@"{longitude}" 
              withString:Longitude]; 

或者乾脆使用+stringWithFormat:

NSString* newURL = [NSString stringWithFormat:@"http://....&lat=%@&long=%@&...", 
               Latitude, Longitude]; 
+0

謝謝,現在一切都清楚了。 :)我會照你的建議去做,並將佔位符更改爲更清晰的值。謝謝! – 2011-02-17 18:22:23

3

這裏是我們開始的地方:

url = @"http://...?ACode=000000000&lat=+&long=-&unittype=1" 
Latitude = @"-33.92" 
Longitude = @"18.42" 

然後你更換的@"+"所有出現的@"-33.92"

url = @"http://...?ACode=000000000&lat=-33.92&long=-&unittype=1" 

然後你更換的@"-"所有出現的@"18.42"請注意,有兩個' - '字符;一個在lat=之後,另一個在long=之後。 'lat'之後的那個是因爲你粘貼的字符串中有一個-

url = @"http://...?ACode=000000000&lat=18.4233.92&long=18.42&unittype=1" 

因此,您的最終結果。

+0

啊,有道理。謝謝! – 2011-02-17 18:22:58

1

您的LAT爲負值。所以 - 被替換兩次。

2

@KennyTM,BJ荷馬和madmik3是正確的。你的價值被替換兩次。

然而,你應該技術上是構建您的網址在一個完全不同的方式:

NSMutableDictionary *query = [NSMutableDictionary dictionary]; 
[query setObject:@"000000000" forKey:@"ACode"]; 
[query setObject:Latitude forKey:@"lat"]; 
[query setObject:Longitude forKey:@"long"]; 
[query setObject:@"1" forKey:@"unittype"]; 

NSMutableArray *queryComponents = [NSMutableArray array]; 
for (NSString *key in query) { 
    NSString *value = [query objectForKey:key]; 
    key = [key stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 
    value = [value stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 

    NSString *component = [NSString stringWithFormat:@"%@=%@", key, value]; 
    [queryComponents addObject:component]; 
} 

NSString *queryString = [components componentsJoinedByString:@"&"]; 

NSString *fullURLString = [NSString stringWithFormat:@"http://api.wxbug.net/getLiveWeatherRSS.aspx?%@", queryString]; 
NSURL *newURL = [NSURL URLWithString:fullURLString]; 

(忽略-stringByAddingPercentEscapesUsingEncoding:對於現在的功效)

的原因,這是更好的是,根據HTTP規範the keys and values in the query of the URL應該是URL encoded。當然,你只是編碼簡單鍵的數字。但如果這種情況發生變化,你的URL可能會中斷。 (這種方法的缺陷是它僅允許每個鍵的單一值,HTTP規範允許您指定多個值。爲了簡便起見,我已經離開了這一點)

也有一些問題在使用-stringByAddingPercentEscapesUsingEncoding:。欲瞭解更多信息,請查看Objective-c iPhone percent encode a string?

+0

戴夫,真棒的建議。現在我不認爲我需要實施它,但我會牢記它。感謝分享。 – 2011-02-17 18:51:58