How can I organize this string better for best coding practices. It’s a string that defines filters:
NSString* string3 = [[[[[[tvA.text stringByReplacingOccurrencesOfString:@"\n" withString:@" "] stringByReplacingOccurrencesOfString:@"&" withString:@"and"] stringByReplacingOccurrencesOfString:@"garçon" withString:@"garcon"] stringByReplacingOccurrencesOfString:@"Garçon" withString:@"Garcon"] stringByReplacingOccurrencesOfString:@"+" withString:@"and"] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
Is there a way to have it be:
NSString* string3 = [[[[[tvA.text filter1] filter2] filter3] filter4] filter5] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
You shouldn’t be replacing & and + before percent-escaping. The problem is that stringByAddingPercentEscapesUsingEncoding: (IIRC) adds the minimum escapes to make it a “valid” URL string, whereas you want to escape anything that might have a special interpretation. For this, use CFURLCreateStringByAddingPercentEscapes():
This encodes & and + correctly, instead of just changing them to “and”. It also encodes newlines as %0a (so you might want to replace them with spaces; that’s your call), and encodes ç as %C3%A7 (which is decoded correctly if you use UTF-8 on the server).