Conversion of objective-c and other C pointers

Source: Internet
Author: User

First, look at the typical nsstring and Cfstringref conversions.

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;

A keyword __bridge appears above, which is the key to the entire transformation. Apple's official explanation for __bridge is as follows:

**__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 is 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.

These words are summed up as:

    • __bridge is used for conversions between OBJECTIVE-C and core foundation pointers, which do not replace ownership of objects (ownership).
    • The __bridge_retained or cfbridgeretain is used for pointer conversions from OBJECTIVE-C to Core Foundation, and the ownership (ownership) of the object is transferred, So you need to call the Cfrelease method to dereference when you are no longer using the object.
    • __bridge_transfer or cfbridgerelease is used to convert a non-objective-c pointer to a objective-c pointer, and the object's ownership (ownership) is given to arc, so you don't have to worry about when the object is released. Just give it to the arc.

Why do I need to call Cfrelease to release an object when I use __bridge_retained for the conversion, in fact the reason is simple: the object of Core Foundation is within the jurisdiction of arc.

Here is the sample code:

Don ' t transfer ownership. You won ' t has toPager' cfrelease ' cfstringref str = (__bridge cfstringref) string;//Transfer ownership (i.e. get ARC out of the). The object is now yours and you mustPager ' cfrelease ' when you ' re do with itcfstringref str = (__bridge_retained cfstringref) string; You'll have the to call ' cfrelease '//Don ' t transfer ownership. ARC stays out of the, 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 has to explicitly call  ' cfrelease ' on  ' str ' nsstring*string = (__bridge_transfer nsstring*) str;    

 

//Don ' t transfer ownership. You won ' t has to call ' cfrelease '

cfstringref str =  ( __bridge CFStringRef )  string ;

//Transfer ownership (i.e. get ARC out of the). The object is now yours, and you must call ' cfrelease ' if you ' re do with it

cfstringref str =  (&NB sp;__bridge_retained cfstringref )  string ; //you'll have the to call ' Cfrelease '

// Don ' t transfer ownership. ARC stays out of the the-the-must call ' cfrelease ' on ' str ' if appropriate (depending on how the ' cfstring ' was creat ed)

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 has to explicitly call ' cfrelease ' on ' str '

nsstring* string =  ( __bridge_ transfer nsstring* )  str ;

Objective-c and other C-pointers conversions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.