Iphone 如何在json URL字符串中包含变量

Iphone 如何在json URL字符串中包含变量,iphone,ios,json,Iphone,Ios,Json,如何将值替换为变量 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

如何将值替换为变量

 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中使用变量。
提前谢谢。

您是否尝试在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:

我不确定我的纬度/经度是否正确,或者它是否必须正确,但无论如何,你明白了!;)

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

- (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]; 
}

感谢您的回复。我在另一个包含2个字符串的url上尝试了类似的操作,但出现了一个错误:方法调用上的参数太多,预期为1,有3个。NSString*urlString=[NSString stringWithFormat:@],pass];这会导致错误。您必须为添加到
-[NSString stringWithFormat:]的每个参数指定一个格式化程序
method。我在另一个包含2个字符串的url上尝试了类似的操作,但出现了一个错误:方法调用上的参数太多,预期为1,有3,这意味着您正在将多个参数传递给
stringWithFormat:
方法。这些参数的格式设置器数量必须与参数数量相同。NSString*urlString=[NSString stringWithFormat:@”,pass];这给了我上述错误。
- (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]; 
}