Conversion between ios oc type and C type

Source: Internet
Author: User

Http://blog.csdn.net/shencaifeixia1/article/details/8147117
_ Bridge: does not involve changes in all Object Relationships_ Bridge_transfer: Give the arc ownership

_ Bridge_retained: removes the arc ownership. Form: (_ Bridge Type) expression; (_ Bridge_retained cf_type) expression; (_ Bridge_transfer objective-C type) expression;

It involves the release of the shared memory pointed to by the converted object pointer and the converted object pointer. Be careful about memory leakage. Generally, _ bridge_retained is only used to convert OC objects to non-OC objects; __bridge can convert each other; __bridge_transfer can only be used to convert non-OC objects to OC objects.

In Arc, when the OC object and non-OC object are forcibly converted, the above three keywords need to be used for bridging. Under what circumstances are the three keywords used? The details are as follows: The following line of code:

Cfstringref S1 = (cfstringref) [[nsstring alloc] initwithformat: @ "Hello, % d !", 1];

The compilation problem will be reported below the arc and a recommended solution will be provided: Cfstringref S1 = (_ bridge cfstringref) [[nsstring alloc] initwithformat: @ "hello, % d !", 1]; Here, nsstring generates the OC object, and the memory is the responsibility of arc. S1 is a CF object, and the memory still needs to be manually managed. You must add a bridge identifier when converting two variables. In this case, no crash or memory leakage occurs. Because the memory generated by alloc is recycled by arc, the relationship between the memory and the memory remains unchanged. If cfrelease (S1) is added later, crash is triggered because the memory is still in the ARC direction and will be excessively released.

Modify:Cfstringref S1 = (_ bridge_retained cfstringref) [[nsstring alloc] initwithformat: @ "hello, % d !", 1];

In this case, the ownership of the object is handed over to the CF object. You need to add cfrelease (S1); for release. Otherwise, leakage will occur.

Let's look at the following code:Cfuuidref UU = cfuuidcreate (null );

Cfstringref S2 = cfuuidcreatestring (null, UU ); Cfrelease (UU );

Nsstring * STR = (_ bridge nsstring *) S2;Nslog (@ "STR: % @", STR );

Cfrelease (S2 ); Both Uu and S2 need to be released using cfrelease because they are not OC objects and are created out of memory, and the ownership is not released.

If you change the following line of code:Nsstring * STR = (_ bridge_transfer nsstring *) S2;

At this time, running the program will cause crash. Because the ownership of S2 has been handed over to the STR in the arc, the arc will release the memory. At this time, calling cfrelease (S2) will cause excessive release. So we should replace this line.

Note:In Arc mode, automatic recovery is only valid for objective-C objects. For core Foundation objects generated using CREATE, copy, and retain, we still need to manually release them, cfrelease ().Summary of _ Bridge:(1) OC Object> non-OC object: the transfer of object ownership is not involved, that is, the ownership of the memory is still handled by arc;(2) Non-OC objects-> OC objects: the transfer of object ownership is not involved, that is, the ownership of the memory is still handled by non-OC objects;

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.