譯者序:
帶學生看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
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