Objective-C和其他C指针的转换
首先看一下典型的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 ; |
Objective-C和其他C指针的转换的更多相关文章
- 对象布局已知时 C++ 对象指针的转换时地址调整
在我调试和研究 netscape 系浏览器插件开发时,注意到了这个问题.即,在对象布局已知(即对象之间具有继承关系)时,不同类型对象的指针进行转换(不管是隐式的从下向上转换,还是强制的从上到下转换)时 ...
- Delphi下使用指针的简单总结(指针的赋值,数组和指针的转换,函数指针的使用)
由于最近公司太忙,好久没有更新我的BLOG了.原来想着写写关于HOOK驱动的文章,可是最后想想好久已经没有做驱动的东西了,怕写出来有错误,于是作罢.开发游戏也有一段时间了,发现使用DELPHI来开发网 ...
- VC++中有关句柄和指针及其转换(转)
原文转自 https://blog.csdn.net/jearmy/article/details/47030011 1.MFC窗口的句柄和指针的转换 (1) 一般窗口对象都会有一个其对应的句柄变量, ...
- void 指针的转换
不论什么类型的指针都能够显式转换为void类型,且不会丢失数据.例如以下面程序: #include<stdio.h> int main(void) { short a=5; void *p ...
- 派生类地址比基类地址少4(子类与基类指针强行转换的时候,值居然会发生变化,不知道Delphi BCB是不是也这样) good
大家对虚表并不陌生,都知道每个含有虚函数的类对象都有1个虚指针,但是在现实使用中,却总是因为这而调试半天,才发现原来是虚指针惹的祸.我这几天在调试代码时候也中招了,我的问题是这样的,如下图,CTree ...
- std::shared_ptr 和普通指针的转换
相互转化见示例 struct test { int num; string name; }; test* pTest = new test(); std::shared_ptr<test> ...
- golang的指针到string,string到指针的转换
由于某个需求,需要如题的转换,废话不多说,直接贴代码了,其实挺丑了,备用了 func (this *Server) socketParserHandler(client *genTcpServer.C ...
- 函数指针的转换 & C的注意点
再让我们看看<signal.h>中声明的signal函数void (*signal(int, void(*)(int)))(int)首先,用typedef简化,typedef void ( ...
- offset 后 指针数组转换
AcDbObjectId pidoffset; AcDbPolyline *plineOffset; AcDbVoidPtrArray ptarr=NULL; pline->getOffsetC ...
随机推荐
- Swift与Objective-c 混编CocoaPods 引入第三方库遇到的问题 (一)
最近Swift 这么火也想尝试着用一下.考虑到Swift 出来的时间也不长.还有就是就是苹果更新的过于平凡 暂时还是不要将现有项目都用swift开发. 先来看看我遇到的问题: 问题一.
- sublime 插件的安装
sublime(text3)插件的安装 之前一直对sublime插件的安装搞不懂,导致自己不能充分地运用它的便捷性.昨天仔细看了下百度,恍然大悟,一下子把必备的插件都装了: 对于插件的安装,首先要在s ...
- [BZOJ 1483][HNOI 2009]梦幻补丁(有序表启发式合并)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1483 分析: 先将不同的颜色的出现位置从小到大用几条链表串起来,然后统计一下答案 对于 ...
- CSharpThinking---C#版本总结(附加三)
C#版本总结: 日期 框架.net Visual Studio C# CLR 2002.2 1.0 2002 1.0 1.0 2003.4 1.1 2003 1.2 1.1 2005.11 2. ...
- python~实现tab补全
文章摘自:http://www.jb51.net/article/58009.htm 第一.如在在vim下实现代码的补全功能. 想要为vim实现自动补全功能,则要下载插件 cd /usr/local/ ...
- Lucene -- 实时索引
lucene的实时搜索可以分成:实时和近实时的搜索. 实时只能依靠内存了. 近实时可以用lucene中提供org.apache.lucene.index.DirectoryReader.open(In ...
- ES6 你可能不知道的事 – 基础篇
序 ES6,或许应该叫 ES2015(2015 年 6 月正式发布),对于大多数前端同学都不陌生. 首先这篇文章不是工具书,不会去过多谈概念,而是想聊聊关于每个特性 你可能不知道的事,希望能为各位同学 ...
- WEB版一次选择多个文件进行批量上传(Plupload)的解决方案
WEB版一次选择多个文件进行批量上传(Plupload)的解决方案 转载自http://www.cnblogs.com/chillsrc/archive/2013/01/30/2883648.htm ...
- CAS做单点登陆(SSO)——集成BIEE 11g
BIEE 11G和CAS集成零代码编写,只需配置. 更改BIEE analytics应用的web.xml 将analytics.war解包(使用7-zip或者Win-rar就可以),然后修改WEB-I ...
- JEECMS页面中常用标签
(1)在段落前给每一行加序号 从0开始:${a_index}从1开始:${a_index+1} (2)标记说明 [文章导航]:[@cms.Position /] [文章标题]:${arti.title ...