標籤:
首先看一下典型的NSString與CFStringRef的相互轉換
http://www.tuicool.com/articles/MJRr226
// CFStringRef to NSString *NSString *yourFriendlyNSString = (__bridge NSString *)yourFriendlyCFString;// NSString * to CFStringRefCFStringRef 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 itCFStringRef 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指標的轉換