博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS 学习日志(5) -----ARC中的_bridge
阅读量:6855 次
发布时间:2019-06-26

本文共 2784 字,大约阅读时间需要 9 分钟。

hot3.png

由于ARC不能管理Core Foundation Object的生命周期,所以当我们在Object-C 和 Core Foundation对象之间转换(id 与 void* 之间的转换)时,我们需要使用到__bridge,__bridge_retained和__bridge_transfer三个转换关键字。

_bridge:只做类型转换,但是不修改对象(内存)所有权。例如:

CFMutableArrayRef cfObject = NULL;{id obj = [[NSMutableArray alloc] init]; // obj has a strong reference to the objectcfObject = (__bridge CFMutableArrayRef)obj; // __bridge does not touch ownership status CFShow(cfObject);printf("retain count = %d\n", CFGetRetainCount(cfObject));} CFRelease(cfObject);输出结果是retain count = 1

_bridge_retained(CFBridgingRetain):将Objective-C的对象转换成Core Fundation的对象,同时获得对象所有权,后续使用CFRealease或其他方法释放对象。(_bridge_retained cast works as if the assigned variable has ownership of the object )例如:

/* ARC */id obj = [[NSObject alloc] init];void *p = (__bridge_retained void *)obj;/* non - ARC */id obj = [[NSObject alloc] init]void *p = obj;[(id)p retain];在ARC中,_bridge_retained 代替了retain,上面的obj与p 都拥有对象的所有权。再如:/* ARC */void *p = 0;{   id obj = [[NSObject alloc] init];   p = (_bridge_retained void*)obj;}NSLog(@"class = %@",[(_bridge id)p class]);上面的代码是有输出的。在大括号结束后,obj的所有权是已经释放了,但是p依然拥有对象的所有权,对象不会释放。/* non-ARC */void *p = 0;{   id obj = [[NSObject alloc] init];  //obj 的retainCount 为1   p = [obj retain];  // obj 的retainCount 为2   [obj release];   // obj 的retainCount 为1   /*[(id)p retainCount] 为1,所有对象是依然存在的 */}NSLog(@"class = %@",[(_bridge id)p class]);

CFBridgingRetain:

CFBridgingRetain的实现方法:CFTypeRef CFBridgingRetain(id X) {return (__bridge_retained CFTypeRef)X;}例子:					CFMutableArrayRef cfObject = NULL;					{id obj = [[NSMutableArray alloc] init];  // obj has a strong reference to the objectcfObject = CFBridgingRetain(obj);        // the object is assigned to cfObjectCFShow(cfObject);   printf("retain count = %d\n", CFGetRetainCount(cfObject));				}printf("retain count after the scope = %d\n", CFGetRetainCount(cfObject));CFRelease(cfObject); 	// the object is discarded输出结果是:retain count = 2;//One is for strong reference of variable obj,the other is by CFBridgingRetainretain count after the scope = 1;//leaving the scope strong reference disappears

_bridge_transfer(CFBridgingRelease):当想把本来拥有对象所有权的变量,在类型转换后,让其释放原先所有权的时候__bridge_transfer cast will release the object just after the assignment is done 

/* ARC */id obj = (__bridge_transfer id)p; /* non-ARC */id obj = (id)p;[obj retain];[(id)p release];

CFBridgingRelease:

CFBridgingRelease的实现方法:					id CFBridgingRelease(CFTypeRef X) {					return (__bridge_transfer id)X;} 		例子:{CFMutableArrayRef cfObject = CFArrayCreateMutable(kCFAllocatorDefault, 0, NULL);printf("retain count = %d\n", CFGetRetainCount(cfObject));id obj = CFBridgingRelease(cfObject);printf("retain count after the cast = %d\n", CFGetRetainCount(cfObject));NSLog(@"class=%@", obj);}

转载于:https://my.oschina.net/u/1040495/blog/307315

你可能感兴趣的文章
ABP官方文档翻译 6.1.2 MVC视图
查看>>
js-监听页面滚动
查看>>
半小时精通PHP正则表达式
查看>>
php一维数组转化二维数组实现数组键值替换及转化json乱码
查看>>
PHP基础2
查看>>
CefSharp.v49.0.1浏览器控件完全WPF版,实现禁止弹出新窗口,在同一窗口打开链接,并且支持带type="POST" target="_blank"的链接...
查看>>
<Android 基础(二十一)> Android 屏幕适配
查看>>
CFile与CStdioFile的区别
查看>>
LNMP的安装--详细版
查看>>
Windows 8 系列(七):使用异步API:await 和 async
查看>>
采坑“微信小程序”
查看>>
Nova官方资料入口处
查看>>
通过GeoIP2分析访问者IP获取地理位置信息
查看>>
20个大背景风格网页设计作品欣赏
查看>>
Google-Authenticator
查看>>
Android开发指南(37) —— Data Backup
查看>>
【VLC-Android】LibVLC API简介(相当于VLC的MediaPlayer)
查看>>
分享一个收集到的文件和目录操作类FileSystemObject
查看>>
团队建设的小技巧
查看>>
laravel 基础知识总结
查看>>