Bridging between the ARC under OC object and CF object (Bridge)

Source: Internet
Author: User

When developing iOS applications we sometimes use core foundation objects called CF, such as Core Graphics, Core Text, and we may need to convert CF objects and OC objects to each other, and we know that The compiler does not automatically manage the memory of the CF object in the ARC environment, so when we create a CF object we need to release it manually using Cfrelease, so how do we manage the memory when CF and OC convert each other? The answer is that we can use __bridge,__bridge_transfer,__bridge_retained when we need it, and the following are the details and usage

The conversion of 1.__BRIDGE:CF and OC objects only involves the conversion of object type not involving object ownership;

NSURL *url = [[NSURL alloc] initWithString:@"http://www.baidu.com"];CFURLRef ref = (CFURLRef)url;

The above code in the ARC environment, the system will give error and error correction, corrected as follows:

NSURL *url = [[NSURL alloc] initWithString:@"http://www.baidu.com"];CFURLRef ref = (__bridge CFURLRef)url;

The system has automatically added __bridge for us because it is an object created by OC and there is no conversion involving object ownership at the time of conversion, so the above code does not need to add cfrelease

2.__bridge_transfer: Often when the CF object is converted to OC objects, the ownership of the CF object is given to the OC object, at which point the arc can automatically manage the memory; (Action with cfbridgingrelease ())

3.__bridge_retained: (contrary to __bridge_transfer) is commonly used when converting OC objects to CF objects, the ownership of the OC object is assigned to the CF object to manage; (function same as cfbridgingretain ())

NSURL *url = [[NSURL alloc] initWithString:@"http://www.baidu.com"ref = (__bridge_retained CFURLRef)url;CFRelease(ref);

When the _bridge_retained identifier is used, it is the OC that will take ownership of the object to the CF object itself to manage, so we will manually release it with cfrelease after the ref use is complete.

CFStringRef cfString= CFURLCreateStringByAddingPercentEscapes(                                                                  NULL,   (__bridge CFStringRef)text,   NULL,                                                                  CFSTR("!*’();:@&=+$,/?%#[]"), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));NSString *ocString = (__bridge_transfer CFStringRef)cfString;

At this point OC obtains the ownership of the object, arc is responsible for the automatic release of the object, if we add cfrelease at the end (cfstring) is purely superfluous, although it will not crash, but the console will print out that the object was free two times.

Copyright NOTICE: This article is for bloggers original article, reproduced please indicate the source.

Bridging between the ARC under OC object and CF object (Bridge)

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.