I use JSON for implementing Facebook in an app and I’m just making my code ARC-friendly. However, when I make lines such as this one
CFStringAppendCharacters((CFMutableStringRef)json, &uc, 1);
become
CFStringAppendCharacters((__bridge CFMutableStringRef)json, &uc, 1);
my app is no longer able to pull my photo albums (I allow the user to log into Facebook and I then display his albums in order for him/her to get a picture to later use in the app).
This is the entire code that is not appreciated by ARC – (could anyone give me a hint how to bridge it please?)
NSString* escaped_value = (NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL, /* allocator */
(CFStringRef)value,
NULL, /* charactersToLeaveUnescaped */
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8);
Does anyone know how I could port the JSON framework for ARC use?
I see in your comment that you just decided to go with
NSJSONSerialization, that will definitely work. But to actually answer your question.Using the
__bridgecast is easy when you think about the memory management involved.__bridgejust casts without doing any memory management operations for you;__bridge_transferalso casts, but it decrements the retain count of the object being casted. So with that in mind, your function call can be broken down like so:Now that you see what’s happening you can safely stack the calls like this:
Note that this method is still not that reliable. A more complete solution can be found in Dave DeLong’s answer to this question.