为 ARC 调整 JSON

发布于 2024-12-27 22:07:52 字数 824 浏览 4 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ら栖息 2025-01-03 22:07:52

我在您的评论中看到您刚刚决定使用 NSJSONSerialization,这肯定会起作用。但要真正回答你的问题。

当您考虑到所涉及的内存管理时,使用 __bridge 转换就很容易了。 __bridge 只是进行转换,而不为您执行任何内存管理操作; __bridge_transfer 也会进行强制转换,但它会减少正在强制转换的对象的保留计数。因此,考虑到这一点,您的函数调用可以像这样分解:

CFStringRef originalValue = (__bridge CFStringRef)value;// Only __bridge required because ownership not changing
CFStringRef escapeChars = (CFStringRef)@"!*'();:@&=+$,/?%#[]";// __bridge not required for string literal
CFStringRef escaped_CFString = CFURLCreateStringByAddingPercentEscapes(NULL, originalValue, NULL, escapeChars, kCFStringEncodingUTF8);// returns a CFStringRef that YOU own.
NSString *escaped_value = (__bridge_transfer NSString *)escaped_CFString; // __bridge_transfer tells the compiler to send a release call to escaped_CFString.

现在您知道发生了什么,您可以安全地堆栈调用,如下所示:

NSString* escaped_value = (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,(__bridge CFStringRef)value,NULL,(CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8);

请注意,此方法仍然不那么可靠。更完整的解决方案可以在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:

CFStringRef originalValue = (__bridge CFStringRef)value;// Only __bridge required because ownership not changing
CFStringRef escapeChars = (CFStringRef)@"!*'();:@&=+$,/?%#[]";// __bridge not required for string literal
CFStringRef escaped_CFString = CFURLCreateStringByAddingPercentEscapes(NULL, originalValue, NULL, escapeChars, kCFStringEncodingUTF8);// returns a CFStringRef that YOU own.
NSString *escaped_value = (__bridge_transfer NSString *)escaped_CFString; // __bridge_transfer tells the compiler to send a release call to escaped_CFString.

Now that you see what's happening you can safely stack the calls like this:

NSString* escaped_value = (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,(__bridge CFStringRef)value,NULL,(CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8);

Note that this method is still not that reliable. A more complete solution can be found in Dave DeLong's answer to this question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文