I'm using CFURLCreateStringByAddingPercentEscapes() because the NSString
version leaves '+' signs unescaped. If I used it directly in code I'd either
CFRelease it, or if assigned to an NSString, __bridge_transfer it. However I
want it as the return value of a method. So I wrote this
+(NSString*)webEncodedString:(NSString *)string
{
CFStringRef retval = CFURLCreateStringByAddingPercentEscapes(
NULL, (__bridge CFStringRef)string, NULL, CFSTR( "!*'();:@&=+$,/?%#[]" ),
kCFStringEncodingUTF8 );
return( __bridge_transfer NSString*)retval;
}
My confusion comes from not knowing whether this function now returns an object
with a +1 refcount or not. If it does it needs to be renamed to have new or
copy etc in the method name, or annotated properly to show that. My first
thought was that yes the transfer means the return value has a +1 refcount.
Then I wondered if the __bridge_transfer transfers ownership to some temporary
inside the method which will be released as it goes out of scope and do the job
so all retains/releases are balanced out and the return value does not have an
extra retain on it. I suppose if I wrote the same thing like this it highlights
the difference
+(NSString*)webEncodedString:(NSString *)string
{
CFStringRef retval = CFURLCreateStringByAddingPercentEscapes(
NULL, (__bridge CFStringRef)string, NULL, CFSTR( "!*'();:@&=+$,/?%#[]" ),
kCFStringEncodingUTF8 );
NSString *temp = (__bridge_transfer)retval;
return temp;
}
Here it would seem the ownership passes to temp, which goes out of scope at the
end of the method and would release, after I assume some retains/autoreleases
are done to ensure the object lives long enough to make it back to the caller,
but still balanced.
Which is it?
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]