I have a NSString which is an address:
开发者_开发知识库"210 Queen Street East Brampton"
I need to ping Google's Geocoding server with a URL constructed from this string that needs to look like this:
http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true_or_false
Clearly, I can append strings together to create a master string called temp and then just
use this code to send the request:
NSMutableString *url = [[NSMutableString alloc] initWithString:temp];
The challenge I am facing is: How do I introduce the '+' signs between words instead of the spaces?
Can anyone suggest if there are built in functions in Objective C that can do this or what's the simplest way to robustly implement this?
Thanks.
I was hoping to find some method that would let you replace characters, but haven't found it yet. The simplest solution I can think of so far would be the following:
NSString *myAddress = @"210 Queen Street East Brampton"; // or whatever the current address is
NSArray *components = [myAddress componentsSeparatedByString:@" "];
// this will strip the words out;
NSString *addressForURL = [components componentsJoinedByString:@"+"];
This should return addressForURL as 210+Queen+Street+East+Brampton. If there is more you need to do with it, this should at least give you a base to start from
精彩评论