Toll-Free Bridging object Bridging (Xcode document translation)

Source: Internet
Author: User

Toll-Free Bridging object Bridging (Xcode document translation)

Preface:

When reading Xcode API documents, students are always confused in full English and asked if there is a Chinese version.

The initial search and query results are in Chinese but few. (If you know where to share this information)

So I am determined to start translating Xcode APIs from today. The topic of this time is Toll-Free Bridging.

 

Toll-Free Bridging

Object 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 caller 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 your strated in the following example.

The Core Foundation framework and Foundation framework have a large number of data types that can be exchanged. This capability is called "Object bridging", which means that you can use the same data type as the parameter called by the Core Foudation function or the recipient of the Objective-C message. For example, NSLocale (see NSLocale Class Reference) is interchangeable with its Core Foundation ing CFLocale (see CFLocale Reference. Therefore, if you see NSLocale * type parameters in the method, you can pass in an NSLocale object. You pass in one type to another to prevent compilation warnings. The Code is as follows:

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.

It is worth noting that memory management functions and methods can also be exchanged-you can allow Cocoa objects to use the CFRelease function and allow Core Foundation objects to use the release and autorelease methods.

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.

 

Note: When the garbage collection mechanism is used, the memory management of the Cocao object and the Core Foundation object is very different. For details, see "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.

Object bridging is available from OS X v10.0. Table 13-1 provides a list of data types that can be exchanged between the Core Fundation framework and the Foundation framework. For each pair, the table also lists the versions available for object bridging in OS X.

Table 13-1Data types that can be used interchangeably between Core Foundation and Foundation

Table 13-1 data objects that can be exchanged between the Core Foundation framework and the Foundation framework

Core Foundation type Foundation class Availability

Available versions of the Core Foundation class (basic class)

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, nsunloop is not toll-free bridged to CFRunLoop, NSBundle is not toll-free bridged to CFBundle, andNSDateFormatter is not toll-free bridged to CFDateFormatter.

 

Note: not all types are object bridging, although their names make them look very similar. For example, the two types of data cannot be bridge objects, the NSBundle and the CFBundle cannot bridge objects, and the NSDateFormatter and the CFDateFormatter cannot bridge objects.

 

Copyright©2012 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.