-2

如何将值替换为变量。

 NSURLRequest *request = [NSURLRequest requestWithURL: [NSURL
 URLWithString:@"https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food"]];

我想更换

1) -33.867
2) 151
3) 500
4) food

带有 URLWithString 中的变量。提前致谢。

4

3 回答 3

2
float latitude = -33.8670522f;
float longitude = 151.1957362f;
int radius = 500;
NSString* types = @"food";

NSString* urlString = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%d&types=%@", latitude, longitude, radius, type];
NSURL* url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

我不确定我的纬度/经度是否正确,反之亦然,但无论如何,你明白了!;)

于 2012-10-19T08:03:44.840 回答
2

为什么不创建某种工厂方法:

- (NSURL *) URLForPlaceForType:(NSString *)typ atLat:(double)lat lng:(double)lng radius:(NSInteger)radius {
    NSString *path = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%d&types=%@", lat, lng, radius, type];
    return [NSURL URLWithString:path]; 
}
于 2012-10-19T08:06:52.750 回答
0

您是否尝试在 NSString 类参考中找到答案?

Finding Characters and Substrings
– rangeOfCharacterFromSet:
– rangeOfCharacterFromSet:options:
– rangeOfCharacterFromSet:options:range:
– rangeOfString:
– rangeOfString:options:
– rangeOfString:options:range:
– rangeOfString:options:range:locale:
– enumerateLinesUsingBlock:
– enumerateSubstringsInRange:options:usingBlock:
于 2012-10-19T08:00:28.183 回答