iphone - stringByReplacingOccurrencesOfString not working as expected -
having problem. here's code:
latitude = [tbxml textforelement:lat]; //latitude & longitude 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 output:
lat:-33.92 lon:18.42 http://api.wxbug.net/getliveweatherrss.aspxacode=000000000&lat=18.4233.92&long=18.42&unittype=1
as can see, strange happening appending code. doing wrong here?
before replacing longitude, string is
http://....&lat=-33.92&long=-&... ^ ^
the system sees there 2 -
, , both of them replaced latitude.
you should use more descriptive string 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 use +stringwithformat:
.
nsstring* newurl = [nsstring stringwithformat:@"http://....&lat=%@&long=%@&...", latitude, longitude];
Comments
Post a Comment