Objective-C和其他C指標的轉換

來源:互聯網
上載者:User

標籤:

首先看一下典型的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指標的轉換

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.