为 ARC 调整 JSON
我使用 JSON 在应用程序中实现 Facebook,我只是让我的代码对 ARC 友好。然而,当我做出这样的行时,
CFStringAppendCharacters((CFMutableStringRef)json, &uc, 1);
我
CFStringAppendCharacters((__bridge CFMutableStringRef)json, &uc, 1);
的应用程序不再能够提取我的相册(我允许用户登录 Facebook,然后我显示他的相册,以便他/她获取一张照片以供以后使用)在应用程序中)。
这是 ARC 不欣赏的整个代码 - (有人可以给我一个提示如何桥接它吗?)
NSString* escaped_value = (NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL, /* allocator */
(CFStringRef)value,
NULL, /* charactersToLeaveUnescaped */
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8);
有谁知道我如何移植 JSON 框架以供 ARC 使用?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在您的评论中看到您刚刚决定使用 NSJSONSerialization,这肯定会起作用。但要真正回答你的问题。
当您考虑到所涉及的内存管理时,使用 __bridge 转换就很容易了。
__bridge
只是进行转换,而不为您执行任何内存管理操作;__bridge_transfer
也会进行强制转换,但它会减少正在强制转换的对象的保留计数。因此,考虑到这一点,您的函数调用可以像这样分解:现在您知道发生了什么,您可以安全地堆栈调用,如下所示:
请注意,此方法仍然不那么可靠。更完整的解决方案可以在Dave DeLong对此问题的回答中找到。
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
__bridge
cast is easy when you think about the memory management involved.__bridge
just casts without doing any memory management operations for you;__bridge_transfer
also 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.