OC 轉(zhuǎn) CF
NSArray *anArray = @[@1,@2,@3,@4];
CFArrayRef aCFArray = (__bridge CFArrayRef)anArray;
NSLog(@"Size of array = %ld",CFArrayGetCount(aCFArray));
__bridge
的意思:ARC 仍具備這個(gè) OC 對(duì)象的保留權(quán)点把。
NSArray *anArray = @[@1,@2,@3,@4];
CFArrayRef aCFArray = (__bridge CFArrayRef)anArray;
NSLog(@"Size of array = %ld",CFArrayGetCount(aCFArray));
CFRelease(aCFArray);
__bridge_retained
: ARC 交出對(duì)象的所有權(quán)爽撒,使用完之后需要調(diào)用CFRelease()。
CF 轉(zhuǎn) OC
使用 __bridge__transfer
在CoreFoundation 創(chuàng)建字典
const void* EOCRetainCallback(CFAllocatorRef allocator, const void *value)
{
return CFRetain(value);
}
void EOCReleaseCallback(CFAllocatorRef allocator, const void *value)
{
CFRelease(value);
}
CFDictionaryKeyCallBacks keyCallbacks = {
0,
EOCRetainCallback,
EOCReleaseCallback,
NULL,
CFEqual,
CFHash
};
CFDictionaryValueCallBacks valueCallbacks = {
0,
EOCRetainCallback,
EOCReleaseCallback,
NULL,
CFEqual
};
CFMutableDictionaryRef aCFDictionary = CFDictionaryCreateMutable(NULL, 10, &keyCallbacks, &valueCallbacks);
NSMutableDictionary *anNSDictionary = (__bridge_transfer NSMutableDictionary*)aCFDictionary;
Foundation層的字典兢哭,其鍵的內(nèi)存管理語(yǔ)義是“拷貝”,值的語(yǔ)義是保留。
如果字典中加入新的鍵值丑慎,就會(huì)調(diào)用retain函數(shù)。
直接在CF層創(chuàng)建字典瓤摧,就能修改鍵值的內(nèi)存管理語(yǔ)義竿裂。