自动释放 CFMutableDictionary

发布于 12-07 07:52 字数 301 浏览 1 评论 0原文

如何自动释放 CFMutableDictionary?

我像这样创建它:

self.mappingByMediatedObject = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

在 dealloc 中:

self.mappingByMediatedObject = nil;

How do I autorelease a CFMutableDictionary?

I'm creating it like this:

self.mappingByMediatedObject = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

And in dealloc:

self.mappingByMediatedObject = nil;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

一杯敬自由2024-12-14 07:52:33

CoreFoundation 没有自动释放的概念——那是 Cocoa 级别的构造。但是,对于 "免费"桥接 跨越字符串和集合类等世界,您可以通过将 CF 引用转换为其相应的 Cocoa 引用,并向其发送 -autorelease 消息,可以获得相同的结果,如下所示:

[(NSDictionary *)aDictionaryRef autorelease];

但是,在您的情况下,您可能并不真正想在此处使用 autorelease,因为您不会将参考资料交还给 Cocoa 调用者。为什么不更明确地分配您的分配,并在设置后释放它,如下所示:

CFDictionaryRef mapping = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
self.mappingByMediatedObject = mapping;
CFRelease(mapping);

CoreFoundation doesn't have a notion of autorelease-- that's a Cocoa-level construct. However, for the objects that are "toll-free" bridged across worlds like strings and the collection classes, you can get the same result by casting the CF reference to its corresponding Cocoa reference, and sending it the -autorelease message, like this:

[(NSDictionary *)aDictionaryRef autorelease];

In your case, though, you might not really want to use autorelease here, because you're not handing back the reference for a Cocoa caller. Why not be a little more explicit around your allocation instead and just releasing it after setting it, like this:

CFDictionaryRef mapping = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
self.mappingByMediatedObject = mapping;
CFRelease(mapping);
流殇2024-12-14 07:52:33

CFDictionaryNSDictionary 是免费桥接的。这意味着 CoreFoundation 对象和它的 Cocoa 对象是可以互换的。

因此,要自动释放 CFDictionary,您可以编写以下内容:

CFDictionary dict = CFDictionaryCreateMutable(...);
self.mappingByMediatedObject = dict;
[(NSDictionary*)dict autorelease];

当然,仅当您的 mappingByMediatedObject 属性保留其值时才自动释放字典 (@property(retain))

CFDictionary and NSDictionary are toll-free bridged. This means the CoreFoundation object and its Cocoa counterpart are interchangable.

So to autorelease a CFDictionary you can write the following:

CFDictionary dict = CFDictionaryCreateMutable(...);
self.mappingByMediatedObject = dict;
[(NSDictionary*)dict autorelease];

Of course autorelease the dictionary only if your mappingByMediatedObject property retains its value (@property(retain))

二货你真萌2024-12-14 07:52:33

从 iOS7 和 Mac OS X 10.9 开始,您可以使用 Core-Foundation 自动释放函数 CFAutorelease()。

As of iOS7 and Mac OS X 10.9 you can use the Core-Foundation auto release function CFAutorelease().

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