• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Objective-C和其他C指针的转换

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

首先看一下典型的NSString与CFStringRef的相互转换

 

 http://www.tuicool.com/articles/MJRr226

// CFStringRef to NSString *
NSString *yourFriendlyNSString = (__bridge NSString *)yourFriendlyCFString;

// NSString * to CFStringRef
CFStringRef yourFriendlyCFString = (__bridge CFStringRef)yourFriendlyNSString;

 

// CFStringRef to NSString *

NSString * yourFriendlyNSString = ( __bridge NSString * ) yourFriendlyCFString ;

// NSString * to CFStringRef

CFStringRef yourFriendlyCFString = ( __bridge CFStringRef ) yourFriendlyNSString ;

上面出现了一个关键字 __bridge ,这个关键字便是整个转换的关键。Apple官方对于 __bridge 的解释如下:

 

**__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 用于Objective-C和Core Foundation指针之间的转换,这种转换不会更换对象的所有权(ownership)。
  • __bridge_retained 或 CFBridgeRetain 用于从Objective-C到Core Foundation的指针转换,并且会将对象的所有权(ownership)转移,所以你需要在不再使用该对象的时候调用CFRelease方法来解除引用。
  • __bridge_transfer 或 CFBridgeRelease 用于将非Objective-C指针转换为Objective-C指针,对象的所有权(ownership)会交给ARC,这时你无须担心对象何时释放,交给ARC去做就好了。

为什么在使用__bridge_retained进行转换时需要自己调用CFRelease来释放对象,其实原因很简单:Core Foundation的对象在ARC的管辖范围之内。

下面是示例代码:

 

 

// Don't transfer ownership. You won't have to call `CFRelease`
CFStringRef str =(__bridge CFStringRef)string;
// Transfer ownership (i.e. get ARC out of the way). The object is now yours and you must call `CFRelease` when you're done with it
CFStringRef str =(__bridge_retained CFStringRef)string; // you will have to call `CFRelease`

// Don't transfer ownership. ARC stays out of the way, and you must call `CFRelease` on `str` if appropriate (depending on how the `CFString` was created)
NSString*string =(__bridge NSString*)str;
// Transfer ownership to ARC. ARC kicks in and it's now in charge of releasing the string object. You won't have to explicitly call `CFRelease` on `str`
NSString*string =(__bridge_transfer NSString*)str;

 

// Don't transfer ownership. You won't have to call `CFRelease`

CFStringRef str = ( __bridge CFStringRef ) string ;

// Transfer ownership (i.e. get ARC out of the way). The object is now yours and you must call `CFRelease` when you're done with it

CFStringRef str = ( __bridge_retained CFStringRef ) string ; // you will have to call `CFRelease`

// Don't transfer ownership. ARC stays out of the way, and you must call `CFRelease` on `str` if appropriate (depending on how the `CFString` was created)

NSString* string = ( __bridge NSString* ) str ;

// Transfer ownership to ARC. ARC kicks in and it's now in charge of releasing the string object. You won't have to explicitly call `CFRelease` on `str`

NSString* string = ( __bridge_transfer NSString* ) str ;


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
objective-c遍历文件夹及判断文件类型发布时间:2022-07-12
下一篇:
Objective-CRuntime使用之全局字体替换为第三方字体(iOS)发布时间:2022-07-12
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap