我想输入类似的内容:
address[address1] = adsads
address[address2] = dsadsad
address[address3] = vadsa
我知道如果这是简单的帖子,我可以这样做:
url?address1=adsads&address2=dsadsa
但如果我想在帖子中将其作为地址对象(数组),我该如何在IOS中创建呢?
答案 0 :(得分:0)
嗯,你可以使用字典。
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:)
答案 1 :(得分:0)
想到使用字典(如果我这样做可能会有效),但只是告诉我可以像以下一样传递网址:
url?address[addres1]=dsadsa&address[address2]=dsadsa
因为浏览器和服务器不会自动编码并将[]作为特殊字符读取。
愚蠢的问题,谢谢你的所有答案
答案 2 :(得分:-1)
假设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;
}
}