Reputation: 6291
Here's the problem... I need to put together some NSStrings
using special characters like "%" or "&". Now here's the code I'm using even though the "%" doesn't show at all:
-(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);
}
This is the result I'd like to get: call
[self getPostingURL:@"http://www.myurl.com/middleInteractionPost.php" variablesNames:@[@"name",@"state",@"language"]];
final string
http://www.myurl.com/middleInteractionPost.php?name=%@&state=%@&language=%@
Upvotes: 0
Views: 2473
Reputation: 22731
Use this code:
-(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);
}
It will construct and print this string:
http://www.myurl.com/middleInteractionPost.php?name=%@&state=%@&language=%@
Upvotes: 1
Reputation: 589
The escape code for a percent sign is "%%", so your code should be replaced with "%%" where you want to show "%".
Upvotes: 3