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