I'm almost certain that I recently saw an networking related method that allowed setting of URL parameters with an NSDictionary.

It was part of the new NSURLSession classes but I can't find it.

Essentially, instead of doing the url like...

@"www.blah.com/something.php?value=18&name=hello"

You could do...

theRequest.parameters = @{@"value":@18, @"name":@"hello"};

Am I imagining this or does this exist?

有帮助吗?

解决方案

Why not write it your self? you can use that, just keep the right order when you put the values into the dictionary.

- (NSString *)makeURLFromDictionary:(NSString *)url values:(NSDictionary *)dictionary
  {
NSMutableString *string = [[NSMutableString alloc]initWithString:url];
[string appendString:@"?"];
for (id key in dictionary) {
    [string appendString:key];
    [string appendString:@"="];
    [string appendString:[dictionary objectForKey:key]];
    [string appendString:@"&"];
}
//this will remove the last &
[string deleteCharactersInRange:NSMakeRange([string length]-1, 1)];

return string;
}

Please just check the returned string, if I'm not wrong you should put it in reverse way, F.I.L.O(First in Last Out style).

enter the dict's values like this:

NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setObject:@"hello" forKey:@"name"];
[dict setObject:@"18" forKey:@"value"];

NSString *urlString = [self makeURLFromDictionary:@"www.blah.com/somthing.php" values:dict];

Hop i help you.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top