标签:
首先看一下典型的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_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 ; |
标签:
原文地址:http://www.cnblogs.com/apem/p/4415063.html