stringByReplacingOccurrencesOfString not working as expected
Having a problem. Here's my code:
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);
And here's the output:
LAT:-33.92 LON:18.42
http://api.wxbug.net/getLiveWeatherRSS.aspxACode=000000000&lat=18.4233.92&long=18.42&unittype=1
As you can see, something strange is happening to the appending code. Am I doing something wrong here?
Before replacing the longitude, the string is
http://....&lat=-33.92&long=-&...
^ ^
The system sees that there are two -
, and thus both of them will be replaced by the latitude.
You should use a more descriptive string to replace with, e.g.
NSString *defaultURL = @"http://....&lat={latitude}&long={longitude}&unittype=1";
newURL = [defaultURL stringByReplacingOccurrencesOfString:@"{latitude}"
withString:Latitude];
newURL = [newURL stringByReplacingOccurrencesOfString:@"{longitude}"
withString:Longitude];
or simply use +stringWithFormat:
.
NSString* newURL = [NSString stringWithFormat:@"http://....&lat=%@&long=%@&...",
Latitude, Longitude];
Here's where we started:
url = @"http://...?ACode=000000000&lat=+&long=-&unittype=1"
Latitude = @"-33.92"
Longitude = @"18.42"
Then you replaced all occurrences of @"+"
with @"-33.92"
:
url = @"http://...?ACode=000000000&lat=-33.92&long=-&unittype=1"
Then you replaced all occurrences of @"-"
with @"18.42"
. Note that there are two '-' characters; one after lat=
and one after long=
. The one after 'lat' is there because the string you pasted in had a -
in it.
url = @"http://...?ACode=000000000&lat=18.4233.92&long=18.42&unittype=1"
Thus, your final result.
@KennyTM, BJ Homer, and madmik3 are correct. Your value is getting replaced twice.
However, you should technically be building your URL in a totally different manner:
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];
(ignoring the efficacy of -stringByAddingPercentEscapesUsingEncoding:
for now)
The reason this is better is that according to the HTTP specification, the keys and values in the query of the URL should be URL encoded. Granted, you're only encoding numbers for simple keys. But if that ever changes, you URL might break. (The flaw with this method is that it only allows a single value per key, and the HTTP spec allows you to specify multiple values. For the sake of simplicity, I've left that out)
There are also some issues on using -stringByAddingPercentEscapesUsingEncoding:
. For more information on that, check out Objective-c iPhone percent encode a string?.
Your LAT is negative. So the - gets replaced twice.
精彩评论