Toll-Free brige: Resolving type conversions between Foundation and Core Foundation

Source: Internet
Author: User

The Core Foundation framework and the Foundation framework have many data types that can be converted to each other, a capability called toll-free bridging. This means that you can use the same type of data as the parameter of the core foundation function call, as well as the return value of the OBJECTIVE-C message. For example Nslocale corresponds to Cflocaleref, but not all data types are toll-free bridged, even if their names appear to be possible. such as Nsrunloop and Cfrunloop,nsbundle and Cfbundle,nsdateformatter and Cfdateformatter.

You need to provide the following information to the compiler when converting:

    • You had to cast one type to the other (type cast)
    • Indicate the object lifetime semantics (provides semantics for the life cycle of objects)

The compiler does not automatically manage the lifecycle of a core foundation object, and you need to use a type cast or a core foundation-style macro to tell the compiler about the ownership semantics of the object.

__bridge

Do not deliver ownership, only convert pointers between objective-c and core Foundation

__bridge_retained/cfbridgingretain

Delivering the objective-c pointer to the core foundation while transferring ownership to the core Foundation, you must call cfrelease or a related function to release ownership

__bridge_transfer/cgbridgingrelease

Deliver non-objective-c pointers to Objective-c while transferring ownership to ARC

Tips:core Foundation in general, such as copy create keyword, should pay attention to release, while converting to OC should pay attention to __bridge_transfer or cgbridgingrelease ()

Example 1:

1Nslocale *gbnslocale = [[Nslocale alloc] Initwithlocaleidentifier:@"EN_GB"];2Cflocaleref Gbcflocale =(__bridge cflocaleref) Gbnslocale;3Cfstringref Cfidentifier =Cflocalegetidentifier (Gbcflocale);4NSLog (@"cfidentifier:%@", (__bridge NSString *) cfidentifier);5 //Logs: "CFIDENTIFIER:EN_GB"6Cflocaleref Mycflocale =cflocalecopycurrent ();7Nslocale *mynslocale = (Nslocale *) cfbridgingrelease (Mycflocale);8NSString *nsidentifier =[Mynslocale Localeidentifier];9Cfshow ((CFSTRINGREF) [@"Nsidentifier:"Stringbyappendingstring:nsidentifier]);Ten //Logs identifier for current locale


Example 2:

1- (void) DrawRect: (cgrect) rect {2Cgcontextref CTX =Uigraphicsgetcurrentcontext ();3Cgcolorspaceref ColorSpace =Cgcolorspacecreatedevicegray ();4CGFloat locations[2] = {0.0,1.0};5Nsmutablearray *colors = [Nsmutablearray arraywithobject: (ID) [[Uicolor Darkgraycolor] cgcolor];6[Colors AddObject: (ID) [[Uicolor Lightgraycolor] cgcolor];7Cggradientref gradient =cggradientcreatewithcolors (ColorSpace, (__bridge cfarrayref) colors, locations);8Cgcolorspacerelease (ColorSpace);//Release owned Core Foundation object.9 TenCgpoint StartPoint = Cgpointmake (0.0,0.0); OneCgpoint EndPoint =Cgpointmake (Cgrectgetmaxx (self.bounds), Cgrectgetmaxy (Self.bounds)); ACgcontextdrawlineargradient (CTX, gradient, startPoint, endpoint,kcggradientdrawsbeforestartlocation |kcggradientdrawsafterendlocation); -Cggradientrelease (gradient);//Release owned Core Foundation object. -}

Toll-Free brige: Resolving type conversions between Foundation and Core Foundation

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.