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
orCFBridgingRetain
casts an Objective-C pointer to a Core Foundation pointer and also transfers ownership to you.You are responsible for callingCFRelease
or a related function to relinquish ownership of the object.__bridge_transfer
orCFBridgingRelease
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 |
Core Foundation type
|
Foundation class
|
Availability
|
---|---|---|
CFArrayRef | NSArray | OS X v10.0 |
CFAttributedStringRef | NSAttributedString | OS X v10.4 |
CFCalendarRef | NSCalendar | OS X v10.4 |
CFCharacterSetRef | NSCharacterSet | OS X v10.0 |
CFDataRef | NSData | OS X v10.0 |
CFDateRef | NSDate | OS X v10.0 |
CFDictionaryRef | NSDictionary | OS X v10.0 |
CFErrorRef | NSError | OS X v10.5 |
CFLocaleRef | NSLocale | OS X v10.4 |
CFMutableArrayRef | NSMutableArray | OS X v10.0 |
CFMutableAttributedStringRef | NSMutableAttributedString | OS X v10.4 |
CFMutableCharacterSetRef | NSMutableCharacterSet | OS X v10.0 |
CFMutableDataRef | NSMutableData | OS X v10.0 |
CFMutableDictionaryRef | NSMutableDictionary | OS X v10.0 |
CFMutableSetRef | NSMutableSet | OS X v10.0 |
CFMutableStringRef | NSMutableString | OS X v10.0 |
CFNumberRef | NSNumber | OS X v10.0 |
CFReadStreamRef | NSInputStream | OS X v10.0 |
CFRunLoopTimerRef | NSTimer | OS X v10.0 |
CFSetRef | NSSet | OS X v10.0 |
CFStringRef | NSString | OS X v10.0 |
CFTimeZoneRef | NSTimeZone | OS X v10.0 |
CFURLRef | NSURL | OS X v10.0 |
CFWriteStreamRef | NSOutputStream | OS X v10.0 |
You can also go through given link: