我几乎可以肯定,我最近看到了一种与网络相关的方法,允许使用NSDictionary设置URL参数。
它是新NSURLSession类的一部分,但我找不到它。
本质上,不是像.
@"www.blah.com/something.php?value=18&name=hello"你可以做..。
theRequest.parameters = @{@"value":@18, @"name":@"hello"};这是我想象的还是真的存在?
发布于 2014-03-06 18:08:03
为什么不自己写呢?您可以使用它,只需在将值放入字典时保持正确的顺序即可。
- (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;
}请检查返回的字符串,如果我没有错,你应该把它以相反的方式,F.I.L.O(先在最后一个输出风格)。
输入dict的值如下:
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];我来帮你。
https://stackoverflow.com/questions/22231537
复制相似问题