剛剛趁有空在翻看蘋果開發(fā)文檔時撑碴,在NSDictionary類的介紹中看到
NSDictionary
istoll-free bridged
with its Core Foundation counterpart,CFDictionaryRef
大概意思是NSDictionary
和它的Core Foundation
的對象CFDictionaryRef
是toll-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框架中有許多數(shù)據類型可以互換使用。該功能被稱為
Toll-Free Bridging
阅羹,意味著你可以把相同的數(shù)據類型作為Core Foundation函數(shù)調用的參數(shù)或者作為OC消息的receiver勺疼。 例如,NSLocale
可以和它的Core Foundation
對象CFLocale
互換捏鱼。因此执庐,當你看到一個方法需要一個NSLocale *
參數(shù),你可以傳入一個CFLocaleRef
,當一個方法需要CFLocaleRef
參數(shù)导梆,你可以傳入一個NSLocate
實例轨淌。你可以把一種類型強制轉換為另一種類型來消除編譯器的警告
蘋果爸爸還很貼心的給出了一個??
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
從示例中可以看到看尼,內存管理函數(shù)和方法也是可以互換的:我們可以將CFRelease
和Cocoa
對象一起使用递鹉,也可以將release``autorelease
和Core Foundation
對象一起使用。
下面這個表格列出了Core Foundation
和Foundation
可以互換的數(shù)據類型
需要注意的是藏斩,并不是所有的數(shù)據類型都是
toll-free bridged
的躏结,盡管它們的名字看起來像是支持toll-free bridged
. 比如,NSRunLoop 和 CFRunLoopRef并不是toll-free bridged
, NSBundle 和 CFBundleRef不是toll-free bridged
, 另外 NSDateFormatter 和 CFDateFormatterRef也不是toll-free bridged
.