views:

332

answers:

1

My iPhone app needs to do some URL escape sequence encoding for a form parameter. The NSString stringByAddingPercentEscapesUsingEncoding as stringByAddingPercentEscapesUsingEncoding doesn't escape a lot. This is what I have

-(NSString*)currentMessageUrlEncoded{
  CFStringRef originalURLString = (CFStringRef) self.currentMessage;
  CFStringRef urlString = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, originalURLString, NULL, (CFStringRef) @"&+", kCFStringEncodingUTF8);  
  NSString *result =  (NSString*) urlString;
  [result autorelease]; // Is this sufficient?
  return result;
}

I'm not sure about the memory management here? Is the autorelease on the result sufficient, or am I barking up the wrong tree altogether?

+3  A: 

Yes it is. You could even shorten you method a bit:

- (NSString *)urlEncodedMessage:(NSString *)message {
    CFStringRef urlString = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef) message, NULL, (CFStringRef) @"&+", kCFStringEncodingUTF8);  
    return [(NSString *)urlString autorelease];
}
nst
Thanks for the answer and the link. (The original method was shorter, btw; I expanded it to emphasise the part about which I was asking).
Paul Wilson