Tuesday 29 April 2014

Toll-Free Bridged Types

There are a number of data types in the Core Foundation framework and the Foundation framework that can be used interchangeably. Data types that can be used interchangeably are also referred to as toll-free bridged data types. This means that you can use the same data structure as the argument to a Core Foundation function call or as the receiver of an Objective-C message invocation. For example, NSLocale (see NSLocale Class Reference) is interchangeable with its Core Foundation counterpart, CFLocale (see CFLocale Reference).
Not all data types are toll-free bridged, even though their names might suggest that they are. For example, NSRunLoop is not toll-free bridged to CFRunLoop, NSBundle is not toll-free bridged to CFBundle, and NSDateFormatter is not toll-free bridged to CFDateFormatter. Table 1 provides a list of the data types that support toll-free bridging.


The compiler does not automatically manage the lifetimes of Core Foundation objects. You tell the compiler about the ownership semantics of objects using either a cast (defined in objc/runtime.h) or a Core Foundation-style macro (defined in NSObject.h):
  • __bridge transfers a pointer between Objective-C and Core Foundation with no transfer of ownership.
  • __bridge_retained or CFBridgingRetain casts an Objective-C pointer to a Core Foundation pointer and also transfers ownership to you.
    You are responsible for calling CFRelease or a related function to relinquish ownership of the object.
  • __bridge_transfer or CFBridgingRelease moves a non-Objective-C pointer to Objective-C and also transfers ownership to ARC.
    ARC is responsible for relinquishing ownership of the object.
Some of these are shown in the following example:


NSLocale *gbNSLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
CFLocaleRef gbCFLocale = (__bridge CFLocaleRef)gbNSLocale;
CFStringRef cfIdentifier = CFLocaleGetIdentifier(gbCFLocale);
NSLog(@"cfIdentifier: %@", (__bridge NSString *)cfIdentifier);
// Logs: "cfIdentifier: en_GB"
 
CFLocaleRef myCFLocale = CFLocaleCopyCurrent();
NSLocale *myNSLocale = (NSLocale *)CFBridgingRelease(myCFLocale);
NSString *nsIdentifier = [myNSLocale localeIdentifier];
CFShow((CFStringRef)[@"nsIdentifier: " stringByAppendingString:nsIdentifier]);
// Logs identifier for current locale


Table 1  Data types that can be used interchangeably between Core Foundation and Foundation
Core Foundation type
Foundation class
Availability
CFArrayRefNSArrayOS X v10.0
CFAttributedStringRefNSAttributedStringOS X v10.4
CFCalendarRefNSCalendarOS X v10.4
CFCharacterSetRefNSCharacterSetOS X v10.0
CFDataRefNSDataOS X v10.0
CFDateRefNSDateOS X v10.0
CFDictionaryRefNSDictionaryOS X v10.0
CFErrorRefNSErrorOS X v10.5
CFLocaleRefNSLocaleOS X v10.4
CFMutableArrayRefNSMutableArrayOS X v10.0
CFMutableAttributedStringRefNSMutableAttributedStringOS X v10.4
CFMutableCharacterSetRefNSMutableCharacterSetOS X v10.0
CFMutableDataRefNSMutableDataOS X v10.0
CFMutableDictionaryRefNSMutableDictionaryOS X v10.0
CFMutableSetRefNSMutableSetOS X v10.0
CFMutableStringRefNSMutableStringOS X v10.0
CFNumberRefNSNumberOS X v10.0
CFReadStreamRefNSInputStreamOS X v10.0
CFRunLoopTimerRefNSTimerOS X v10.0
CFSetRefNSSetOS X v10.0
CFStringRefNSStringOS X v10.0
CFTimeZoneRefNSTimeZoneOS X v10.0
CFURLRefNSURLOS X v10.0
CFWriteStreamRefNSOutputStreamOS X v10.0
You can also go through given link:


No comments:

Post a Comment