Autorelease CFMutableDictionary
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;
开发者_开发知识库
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);
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)
)
As of iOS7 and Mac OS X 10.9 you can use the Core-Foundation auto release function CFAutorelease().
精彩评论