標籤:__bridge objective-c對象與core f
Core Foundation 對象主要使用在用C語言編寫的Core Foundation 架構中,並引用計數的對象.與Objective-C對象區別很少.無論哪種架構產生的對象,一旦產生,便可在兩種架構中使用.這種轉換不需要額外的CPU資源,因而也被稱為”免費橋”.
/****************************(1)***********************/
/*
CFTypeRef CFBridgingRetain(id X){
return (__bridge_retained CFTypeRef)X;
}
id CFBridgingRelease(CFTypeRef X){
return (__bridge_transfer id)X;
}
*/
CFMutableArrayRef cfObject = NULL;
{
id obj = [[NSMutableArrayalloc] init];
cfObject =CFBridgingRetain(obj);
//也可使用 cfObject = (__bridge_retained CFMutableArrayRef)obj;
/*
ARC機制在 Core Foundation架構是否有效?
輸出列印如下:
retain count = 2
retain count = 1
由此可知ARC機制在Core Foundation下並不起作用
*/
CFShow(cfObject);
printf("retain count = %ld\n",CFGetRetainCount(cfObject));
}
printf("retain count = %ld\n",CFGetRetainCount(cfObject));
CFRelease(cfObject);
/****************************(2)***********************/
//使用__bridge轉換來替代CFBridgingRetain或__bridge_retained轉換
/*
cfObject = (__bridge CFMutableArrayRef)obj;
但使用__bridge並不改變對象的持有情況.
*/
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
Objective-C對象與Core Foundation對象