2011-02-17 115 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