我想输入如下内容:
address[address1] = adsads
address[address2] = dsadsad
address[address3] = vadsa
我知道如果这是简单的帖子,我可以这样做:
url?address1=adsads&address2=dsadsa
但是如果我想在帖子中使它成为地址对象(数组),我该如何在IOS中制作它?
想过使用字典(如果我这样做的话可能会起作用),但只是被告知我可以像这样传递 url:
url?address[addres1]=dsadsa&address[address2]=dsadsa
因为浏览器和服务器不会自动对其进行编码并将 [] 作为特殊字符读取。
愚蠢的问题,谢谢大家的回答
嗯,你可以用字典。
NSMutableDictionary * argDict = [NSMutableDictinary new];
[argDict setObject:@"John" forKey:@"name"];
[argDict setObject:@"26" forKey:@"age"];
...
NSMutableString * url = [NSMutableString stringWithCapacity:30];
[url appendString:@"http://?"];
BOOL isFirstArg = YES;
for (NSString * key in [argDict allKeys]) {
if (isFirstArg) {
[url appendFormat:@"%@=%@", key, [argDict objectForKey:key]];
isFirstArg = NO;
continue;
}
[url appendFormat:@"&%@=%@", key, [argDict objectForKey:key]];
}
注意:我想你也可以找到一种更好的方法来处理第一个 arg :)
假设 address1,address2,address3, ... 等是整数,您可以使用 switch 如下所示:
for (int i = 0; i < addressX; i++)
{
switch(i)
{
case address1:
address[i]=adsads;
break;
case address2:
address[i]=aoao;
break;
case address3:
address[i]=olol;
break;
case address4:
address[i]=dodo;
break;
case address5:
address[i]=lolo;
break;
}
}