问题在于......我需要使用#34;%"等特殊字符组合一些NSStrings
。或"&"。现在,即使是"%"根本不显示:
-(void)getPostingURL:(NSString*)path variablesNames:(NSArray*)names{
NSString *result = path;
for (int i=0; i<names.count; i++){
result = [NSString stringWithFormat:@"%@?%@=&@&",result,names[i]];
}
NSLog(result);
}
这是我想得到的结果: 调用
[self getPostingURL:@"http://www.myurl.com/middleInteractionPost.php" variablesNames:@[@"name",@"state",@"language"]];
最终字符串
http://www.myurl.com/middleInteractionPost.php?name=%@&state=%@&language=%@
答案 0 :(得分:2)
百分号的转义码为“%%”,因此您的代码应替换为“%%”,并显示“%”。
答案 1 :(得分:0)
使用此代码:
-(void)getPostingURL:(NSString*)path variablesNames:(NSArray *)names
{
NSString *result = path;
result = [result stringByAppendingString:@"?"];
for (NSString *variableName in names){
NSString *stringToAppend = [NSString stringWithFormat:@"%@=%%@", variableName];
result = [result stringByAppendingString:stringToAppend];
if (![variableName isEqual:[names lastObject]]) {
result = [result stringByAppendingString:@"&"];
}
}
NSLog(@"%@", result);
}
它将构造并打印此字符串:
http://www.myurl.com/middleInteractionPost.php?name=%@&state=%@&language=%@