Question

I am trying to url encode a string in my iOS 5 app using ARC.

This is how i do it:

- (NSString *)escape:(NSString *)text
{
    return (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
                                          (__bridge CFStringRef)text, NULL,
                                          (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                          kCFStringEncodingUTF8);
}

I then call it with test data like this:

NSLog([self escape:@"kalel///&&&???"]);

But the output i get from the NSLog is this:

kalel0.0000000.0000000.00000022623F0.0000000.000000

That just does not seem right, but no matter what I can't get it right

Was it helpful?

Solution

Your escape function is fine. The problem is in a way you call NSLog:

escape produces the string kalel%2F%2F%2F%26%26%26%3F%3F%3F for your input. NSLog interprets this string as a format string and prints some garbage as floating point numbers right after kalel word!

You should always call NSLog with a string constant as a first argument, e.g.:

NSLog(@"%@", [self escape:@"kalel///&&&???"]);

P.S. You have a memory leak in escape --- you should return (__bridge_transfer NSString *) as you transfer retained CF object to Objective C space.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top