首页 > 其他 > 详细

__bridge,__bridge_retained和__bridge_transfer的意思,区别与使用

时间:2014-08-04 17:11:57      阅读:372      评论:0      收藏:0      [点我收藏+]

最近碰到了CFObject和NSObject转换的问题,由于ARC不能管理Core Foundation Object的生命周期,所以在Core Foundation和ARC之间,我们需要使用到__bridge,__bridge_retained和__bridge_transfer三个转换关键字。

根据苹果官方的文档(https://developer.apple.com/library/ios/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html):

    • __bridge transfers a pointer between Objective-C and Core Foundation with no transfer of ownership.

    • __bridge_retained or CFBridgingRetain casts an Objective-C pointer to a Core Foundation pointer and also transfers ownership to you.

      You are responsible for calling CFRelease or a related function to relinquish ownership of the object.

    • __bridge_transfer or CFBridgingRelease moves a non-Objective-C pointer to Objective-C and also transfers ownership to ARC.

      ARC is responsible for relinquishing ownership of the object.

意思就是:

__bridge只做类型转换,但是不修改对象(内存)管理权;

__bridge_retained(也可以使用CFBridgingRetain)将Objective-C的对象转换为Core Foundation的对象,同时将对象(内存)的管理权交给我们,后续需要使用CFRelease或者相关方法来释放对象;

__bridge_transfer(也可以使用CFBridgingRelease)将Core Foundation的对象转换为Objective-C的对象,同时将对象(内存)的管理权交给ARC。

__bridge,__bridge_retained和__bridge_transfer的意思,区别与使用,布布扣,bubuko.com

__bridge,__bridge_retained和__bridge_transfer的意思,区别与使用

原文:http://www.cnblogs.com/xiaochaozi/p/3890254.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!