ios-Bridge Connection method

Source: Internet
Author: User

Many times the corefoundation function is used, and its return value is the object of the corefoundation frame, which can be done using bridging if the object you want to convert to the foundation frame.

Example code:

CFSTRINGREF FirstName = abrecordcopyvalue (person, kabpersonfirstnameproperty);   = Abrecordcopyvalue (person, kabpersonlastnameproperty);         *firstname = (__bridge NSString *) (firstName);   *lastname = (__bridge NSString *) (lastName);  

However, there is a possibility of a memory leak using this bridging method.

Functions that use Corefoundation in Arc mode need to take into account the memory leak problem, the above bridge method is simply to give the Corefoundation framework's object ownership to the foundation framework to use, and cannot manage the object's memory, If we want to manage this object's memory, we can use a different kind of bridging method.

Example code:

NSString *firstname = (__bridge_transfer NSString *) (firstName);   *lastname = (__bridge_transfer NSString *) (lastName);  
Summarize:
    1. __bridge Type: The object ownership of the Corefoundation framework is given to the foundation framework for use, but the objects in the foundation framework cannot manage the memory of the object.
    2. __bridge_transfer objective-c Type: The object ownership of the Corefoundation framework is assigned to the foundation to manage, if the object in the foundation is destroyed, then our previous object ( Corefoundation) will be destroyed together.

Using the __bridge_transfer objective-c type Bridge, you will no longer have to manually manage the memory yourself!

ios-Bridge Connection method

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.