首页 > 代码库 > Toll-Free Bridging 对象桥接(Xcode文档翻译)
Toll-Free Bridging 对象桥接(Xcode文档翻译)
译者序:
带学生看Xcode API 文档的时候,总有学生被通篇的英文搞晕,并询问是否有中文版。
初步搜索和询问的结果是,中文版有但是很少。(如果知道哪里有,希望能够分享一下这个信息)
于是决心从今天开始对Xcode API进行翻译,这次的主题是"Toll-Free Bridging"。
英文原版版权归苹果公司所有,翻译内容仅供学习参考。
首发网址:http://blog.csdn.net/duzixi/article/details/38325021
-----------------------------------------------
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 anNSLocale * 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
CFErrorRef NSError OS X v10.5
CFLocaleRef NSLocale OS X v10.4
CFMutableArrayRef NSMutableArray OS X v10.0
CFMutableAttributedStringRef NSMutableAttributedStringOS X v10.4
CFMutableCharacterSetRefNSMutableCharacterSet 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
Note: 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, andNSDateFormatter is not toll-free bridged to CFDateFormatter.
注意:并不是所有的类型都是对象桥接,尽管其名称让它们看上去很像。例如,NSRunLoop和CFRunLoop不能对象桥接,NSBundle和CFBundle不能对象桥接,NSDateFormatter和CFDateFormatter不能对象桥接等。
Copyright ? 2012 Apple Inc. All Rights Reserved.Terms of Use |Privacy Policy | Updated: 2012-01-09