译者序:
带学生看Xcode API 文档的时候,总有学生被通篇的英文搞晕,并询问是否有中文版。
初步搜索和询问的结果是,中文版有但是很少。(如果知道哪里有,希望能够分享一下这个信息)
于是决心从今天开始对Xcode API进行翻译,这次的主题是Toll-Free Bridging。
?
Toll-Free Bridging
对象桥接
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 aCFLocaleRef, and in a function where you see aCFLocaleRef parameter, you can pass anNSLocale instance. You cast one type to the other to suppress compiler warnings, as illustrated in the following example.
Core Foundation框架和Foundation框架中有大量的数据类型能被可交换的使用。这个能力被称之为“对象桥接”,这就意味着你可以使用相同的数据类型作为Core Foudation函数调用的参数或者Objective-C消息的接受者。例如,NSLocale(参见NSLocale Class Reference)和它Core Foundation对应者CFLocale(参见 CFLocale Reference)是可交换的。因此,如果你在方法中看到了NSLocale *类型的参数,你就可以传入一个NSLocale对象。你通过传入一个类型到另一个去阻止编译警告,代码如下:
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
?
Note from the example that the memory management functions and methods are also interchangeable—you can useCFRelease with a Cocoa object andrelease andautorelease with a Core Foundation object.
值得注意的是,内存管理函数和方法也可交换的——你可以让Cocoa对象去使用CFRelease函数并且让Core Foundation对象去使用release和autorelease方法。
Note: When using garbage collection, there are important differences to how memory management works for Cocoa objects and Core Foundation objects. See “Using Core Foundation with Garbage Collection” for details.
?
注意:当使用垃圾回收机制时,Cocao对象和Core Foundation对象的内存管理有很大的不同。详细请参见“Using Core Foundation with Garbage Collection”。
?
Toll-free bridging has been available since OS X v10.0. Table 13-1 provides a list of the data types that are interchangeable between Core Foundation and Foundation. For each pair, the table also lists the version of OS X in which toll-free bridging between them became available.
对象桥接从OS X v10.0版本开始可用。表格 13 - 1 提供了能在Core Fundation框架和Foundation框架之间交换使用的数据类型列表。对于每一对,表格也列出了 OS X中对象桥接可用的版本信息。
Table 13-1 Data types that can be used interchangeably between Core Foundation and Foundation
表格 13-1 能够在Core Foundation框架和Foundation框架之间交换使用的数据对象
Core Foundation type Foundation class Availability
Core Foundation类型 Foundation类(基础类) 可用版本
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
CFError