Toll-Free Bridge

Core Foundation 和Foundation框架的转换桥

一下函数可用于OC对象与CoreFoundation对象之间的变换,即 Toll-Free Bridge

`CFTypeRef CFBridgeRetain (id X)
{
return (__bridge_retained CFTreeRef)X;
}

id CFCFBridgeRelease (CFTypeRef X) {
return (__bridge_transfer id)X;
}

`

一下生成并持有的NSMutableArray对象座位Core Foundation对象来处理

`
CFMutableArrayRef cfObject = NULL;
{
//变量obj持有对象生成并持有对象的强引用
id obj = [NSMutableArrayRef alloc]init;
//通过CFBridgeRetain将对象CFretain赋值给变量cfObject

cfObject = CFBridgeRetain(obj);

//通过变量obj的强引用和通过CFBridgeRetain 引用计数为2
CFShow (cfObject);
}
//因为变量obj超出作用域,强引用失效,对象得到释放,无持有者的对象被废弃

//此后对象访问出错 (悬垂指针)
CFRelease(cfObject);
`

你可能感兴趣的:(Toll-Free Bridge)