首先看一下典型的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指针的转换的更多相关文章

  1. 对象布局已知时 C++ 对象指针的转换时地址调整

    在我调试和研究 netscape 系浏览器插件开发时,注意到了这个问题.即,在对象布局已知(即对象之间具有继承关系)时,不同类型对象的指针进行转换(不管是隐式的从下向上转换,还是强制的从上到下转换)时 ...

  2. Delphi下使用指针的简单总结(指针的赋值,数组和指针的转换,函数指针的使用)

    由于最近公司太忙,好久没有更新我的BLOG了.原来想着写写关于HOOK驱动的文章,可是最后想想好久已经没有做驱动的东西了,怕写出来有错误,于是作罢.开发游戏也有一段时间了,发现使用DELPHI来开发网 ...

  3. VC++中有关句柄和指针及其转换(转)

    原文转自 https://blog.csdn.net/jearmy/article/details/47030011 1.MFC窗口的句柄和指针的转换 (1) 一般窗口对象都会有一个其对应的句柄变量, ...

  4. void 指针的转换

    不论什么类型的指针都能够显式转换为void类型,且不会丢失数据.例如以下面程序: #include<stdio.h> int main(void) { short a=5; void *p ...

  5. 派生类地址比基类地址少4(子类与基类指针强行转换的时候,值居然会发生变化,不知道Delphi BCB是不是也这样) good

    大家对虚表并不陌生,都知道每个含有虚函数的类对象都有1个虚指针,但是在现实使用中,却总是因为这而调试半天,才发现原来是虚指针惹的祸.我这几天在调试代码时候也中招了,我的问题是这样的,如下图,CTree ...

  6. std::shared_ptr 和普通指针的转换

    相互转化见示例 struct test { int num; string name; }; test* pTest = new test(); std::shared_ptr<test> ...

  7. golang的指针到string,string到指针的转换

    由于某个需求,需要如题的转换,废话不多说,直接贴代码了,其实挺丑了,备用了 func (this *Server) socketParserHandler(client *genTcpServer.C ...

  8. 函数指针的转换 & C的注意点

    再让我们看看<signal.h>中声明的signal函数void (*signal(int, void(*)(int)))(int)首先,用typedef简化,typedef void ( ...

  9. offset 后 指针数组转换

    AcDbObjectId pidoffset; AcDbPolyline *plineOffset; AcDbVoidPtrArray ptarr=NULL; pline->getOffsetC ...

随机推荐

  1. WPF开发时光之痕日记本(二)—— MVVM基类

    当我们用MVVM的时候要实现INotifyPropertyChanged,每次都要实现这个接口比较麻烦,所以基类的作用就体现出来了.代码如下: public class ViewModelBase : ...

  2. DateTime.Parse

    上月第一天:DateTime.Parse(DateTime.Now.AddMonths(-1).ToString("yyyy-MM-01")) 上周星期天:DateTime.Par ...

  3. 字符串去掉空格 trim()方法

    jquery库提供了$.trim()方法,能直接用, 但没用库时FF里有效果,IE里就没实现, 解决办法:用正则替换 方法: function trimStr(str){return str.repl ...

  4. dp式子100个……

    1.        资源问题1-----机器分配问题F[I,j]:=max(f[i-1,k]+w[i,j-k]) 2.        资源问题2------01背包问题F[I,j]:=max(f[i- ...

  5. [C#]exchange发送,收件箱操作类

    最近项目中需要用到exchange的操作,就参照msdn弄了一个简单的操作类.目前先实现了,发送邮件和拉取收件箱的功能,其他的以后在慢慢的添加. using Microsoft.Exchange.We ...

  6. Java常见异常

    1. runtimeException子类: 1. java.lang.ArrayIndexOutOfBoundsException    数组索引越界异常.当对数组的索引值为负数或大于等于数组大小时 ...

  7. 【kAriOJ】离散数学春季学期编程测试 1

    A.凯撒密码 题意: 给你k1,k2,和一串明文,一串密文. 明文用k1加密,密文用k2解密. 对于明文要把字母转换成大写字母,非字母全部删除. 额:要考虑到取模可能会变成负数,所以要加一下26再取模 ...

  8. 【CodeForces 297C】Splitting the Uniqueness

    题意 序列s有n个数,每个数都是不同的,把它每个数分成两个数,组成两个序列a和b,使ab序列各自去掉个数后各自的其它数字都不同. 如果存在一个划分,就输出YES,并且输出两个序列,否则输出NO. 分析 ...

  9. NOI2002 洛谷 P1196 银河英雄传说

    神奇的并查集问题 题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦 创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩 ...

  10. POJ2965The Pilots Brothers' refrigerator(枚举+DFS)

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22057 ...