如何将格式说明符添加到NSURL



我有以下代码,用于发出Google Places API请求。此时参数是静态设置的。我该如何将这些参数(类型、lat/lon和Google Key——我在.h文件中将其定义为常量)作为对象?

我的问题出现在NSURL上,因为我无法添加格式说明符

谢谢你的帮助。

-(void)ParseXML_of_Google_PlacesAPI
{
    NSURL *googlePlacesURL = [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/xml?location=34.0522222,-118.2427778&radius=500&types=bar&sensor=false&key=MyGoogleAPIKey"];
    NSData *xmlData = [NSData dataWithContentsOfURL:googlePlacesURL];
    xmlDocument = [[GDataXMLDocument alloc]initWithData:xmlData options:0 error:nil];
    NSArray *arr = [xmlDocument.rootElement elementsForName:@"result"];
    for(GDataXMLElement *e in arr )
    {
        [placesOutputArray addObject:e];
    }

好的'stringWithFormat

`NSString* urlToCall = [NSString stringWithFormat:@"http:://url.to.webservice/api?param1=%@&param2=%@", param1, param2]`

这可能对有帮助

float lat=34.0522222,lon=-118.2427778;
NSString *typestr=@"bar";
NSString *key=@"MyGoogleAPIKey";

NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/xml?location=%f,%f&radius=500&types=%@&sensor=false&key=%@",lat,lon,typestr,key]];
NSLog(@"url values  ==%@",url);

将说明符直接格式化为NSURL,如下所示:

 NSURL *googlePlacesURL = [NSURL URLWithString:[NSString stringWithFormat:@"http:://url.to.webservice/api?param1=%@%param2=%@", param1, param2]];

最新更新