自动释放 CFMutableDictionary
如何自动释放 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 技术交流群。
发布评论
评论(3)
流殇2024-12-14 07:52:33
CFDictionary
和 NSDictionary
是免费桥接的。这意味着 CoreFoundation 对象和它的 Cocoa 对象是可以互换的。
因此,要自动释放 CFDictionary
,您可以编写以下内容:
CFDictionary dict = CFDictionaryCreateMutable(...);
self.mappingByMediatedObject = dict;
[(NSDictionary*)dict autorelease];
当然,仅当您的 mappingByMediatedObject
属性保留其值时才自动释放字典 (@property(retain)
)
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
CoreFoundation 没有自动释放的概念——那是 Cocoa 级别的构造。但是,对于 "免费"桥接 跨越字符串和集合类等世界,您可以通过将 CF 引用转换为其相应的 Cocoa 引用,并向其发送
-autorelease
消息,可以获得相同的结果,如下所示:但是,在您的情况下,您可能并不真正想在此处使用 autorelease,因为您不会将参考资料交还给 Cocoa 调用者。为什么不更明确地分配您的分配,并在设置后释放它,如下所示:
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: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: