Toll-Free Bridging

刚刚趁有空在翻看苹果开发文档时,在NSDictionary类的介绍中看到

NSDictionary is toll-free bridged with its Core Foundation counterpart,CFDictionaryRef

大概意思是NSDictionary和它的Core Foundation的对象CFDictionaryReftoll-free bridged

toll-free bridged是什么意思? 免费桥接? 等价交换?

懵逼

然后好奇点了roll-free bridged,看了下它的官方文档。

大概摘抄如下:

There are a number of data types in the Core Foundation framework and the Foundation framework that can be used interchangeably. This capability, called toll-free bridging, means that you can use the same data type as the parameter to a Core Foundation function call or as the receiver of an Objective-C message. For example, NSLocale (see NSLocale Class Reference) is interchangeable with its Core Foundation counterpart, CFLocale (see CFLocale Reference). Therefore, in a method where you see an NSLocale * parameter, you can pass a CFLocaleRef, and in a function where you see a CFLocaleRef parameter, you can pass an NSLocale instance. You cast one type to the other to suppress compiler warnings, as illustrated in the following example.

大致意思是:

Core Foundation框架和Foundation框架中有许多数据类型可以互换使用。该功能被称为Toll-Free Bridging,意味着你可以把相同的数据类型作为Core Foundation函数调用的参数或者作为OC消息的receiver。 例如,NSLocale可以和它的Core Foundation对象CFLocale互换。因此,当你看到一个方法需要一个NSLocale *参数,你可以传入一个CFLocaleRef,当一个方法需要CFLocaleRef参数,你可以传入一个NSLocate实例。你可以把一种类型强制转换为另一种类型来消除编译器的警告

苹果爸爸还很贴心的给出了一个


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

打印:

cfIdentifier: en_GB
nsIdentifier: en_US

从示例中可以看到,内存管理函数和方法也是可以互换的:我们可以将CFReleaseCocoa对象一起使用,也可以将release``autoreleaseCore Foundation对象一起使用。

下面这个表格列出了Core FoundationFoundation可以互换的数据类型

Core Foundation type Foundation class
CFArrayRef NSArray
CFAttributedStringRef NSAttributedString
CFBooleanRef NSNumber
CFCalendarRef NSCalendar
CFCharacterSetRef NSCharacterSet
CFDataRef NSData
CFDateRef NSDate
CFDictionaryRef NSDictionary
CFErrorRef NSError
CFLocaleRef NSLocale
CFMutableArrayRef NSMutableArray
CFMutableAttributedStringRef NSMutableAttributedString
CFMutableCharacterSetRef NSMutableCharacterSet
CFMutableDataRef NSMutableData
CFMutableDictionaryRef NSMutableDictionary
CFMutableSetRef NSMutableSet
CFMutableStringRef NSMutableString
CFNullRef NSNull
CFNumberRef NSNumber
CFReadStreamRef NSInputStream
CFRunLoopTimerRef NSTimer
CFSetRef NSSet
CFStringRef NSString
CFTimeZoneRef NSTimeZone
CFURLRef NSURL
CFWriteStreamRef NSOutputStream

需要注意的是,并不是所有的数据类型都是toll-free bridged的,尽管它们的名字看起来像是支持toll-free bridged. 比如,NSRunLoop 和 CFRunLoopRef并不是toll-free bridged, NSBundle 和 CFBundleRef不是toll-free bridged, 另外 NSDateFormatter 和 CFDateFormatterRef也不是toll-free bridged.

end

你可能感兴趣的:(Toll-Free Bridging)