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 = [[NSMutableArray alloc] 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 ",CFGetRetainCount(cfObject));
}
printf("retain count = %ld ",CFGetRetainCount(cfObject));
CFRelease(cfObject);
/****************************(2)***********************/
//使用__bridge转换来替代CFBridgingRetain或__bridge_retained转换
/*
cfObject = (__bridge CFMutableArrayRef)obj;
但使用__bridge并不改变对象的持有情况.
*/