As Justin pointed out, there is a much simpler way of doing this. NSString has a -initWithFormat:arguments: method that does exactly what you want.
Also, your method name has a few issues:
- Naming convention - you should indicate in the method name its purpose (creating a URL request)
- You are passing in an (NSDictionary *) for the path, but casting it to an (NSString *) when you use it. The two objects are not type compatible. I'm supposing this might be a typo when you copy-pasted your code?
- Might as well use the same calling convention as NSString's +stringWithFormat: method.
Given all of the above, the method becomes something like (without error checking):
- (NSURLRequest *)URLRequestWithFormat:(NSString *)format, ... {
va_list arguments;
va_start(arguments, format);
NSString *urlPath = [[NSString alloc] initWithFormat:format arguments:arguments];
va_end(arguments);
NSURL *url = [NSURL URLWithString:urlPath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
return request;
}
This worked fine with a call like:
NSURLRequest *request = [self URLRequestWithFormat:@"https://api.foursquare.com/v2/venues/categories/client_id=%@&client_secret=%@&v=%@", @"One",@"Two",@"Three"];
NSLog(@"Request: %@", request);
With output:
2014-05-07 09:52:30.645 Test[5888:60b] Request: <NSURLRequest: 0x8c64f30> { URL: https://api.foursquare.com/v2/venues/categories/client_id=One&client_secret=Two&v=Three }