Free bridging in iOS and free bridging in iOS

Source: Internet
Author: User

Free bridging in iOS and free bridging in iOS

Excerpted from iOS 6 programming practices.

Unlike the Objective-C library, the standard C language and Core Foundation Class Library (CF * method) in Objective-C do not follow those naming conventions. This means that the ARC compiler cannot release unwanted memory. Before the emergence of ARC, We can forcibly convert the CF * object to the NS * object, which is called toll-free bridging ). That is to say, we can convert the CFString * type to the NSString * type through type conversion. With ARC, we can no longer do this, at least not specify a ownership transfer modifier.

ARC allows the following ownership transfer modifiers: __bridge, _ bridge_retained, _ bridge_transfer

1. _ bridge

The first modifier _ bridge is a normal conversion, which tells ARC not to increase its reference count value or change its ownership. LLVM designers didn't make _ bridge the default ownership transfer modifier, because the decision was too bold. The Core Foundation method does not have an appropriate naming convention. Therefore, without explicitly specifying a ownership transfer modifier, the compiler cannot make a decision on how to transfer ownership of a C pointer type.

2. _ bridge_retained

To convert the C pointer type and increase the reference count value, we can use the second modifier. This modifier can be used to return a reserved pointer from the Objective-C method (which creates a Core Foundation object and releases it later using the CFRelease method. If the Objective-C method belongs to the NSRETURNS_RETAINED series, a reserved pointer is returned.

3. _ bridge_transfer

If you want to convert the Core Foundation pointer type to an Objective-C pointer and Add 1 to the reference count, you can use the last modifier. If the Core Foundation method creates an object and wants to use ARC to manage the object's memory, we can use this modifier. Essentially, we "transfer" ownership to ARC.

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.