Toll-Free Bridging

Toll-Free Bridging,第1张

概述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

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.

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
NSLocale *gbNSLocale = [[NSLocale alloc] initWithLocaleIDentifIEr:@"en_GB"];

Note from the example that the memory management functions and methods are also interchangeable—you can use CFRelease with a Cocoa object and release and autorelease with a Core Foundation object.

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.

Toll-free brIDging has been available since Mac 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 Mac OS X in which toll-free brIDging between them became available.

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

Core Foundation type

Foundation class

Availability

CFArrayRef

NSArray

Mac OS X v10.0

CFAttributedStringRef

NSAttributedString

Mac OS X v10.4

CFCalendarRef

NSCalendar

CFCharacterSetRef

NSCharacterSet

CFDataRef

NSData

CFDateRef

NSDate

CFDictionaryRef

NSDictionary

CFErrorRef

NSError

Mac OS X v10.5

CFLocaleRef

NSLocale

CFMutableArrayRef

NSMutableArray

CFMutableAttributedStringRef

NSMutableAttributedString

CFMutableCharacterSetRef

NSMutableCharacterSet

CFMutableDataRef

NSMutableData

cfmutabledictionaryRef

NSMutableDictionary

CFMutableSetRef

NSMutableSet

CFMutableStringRef

NSMutableString

CFNumberRef

NSNumber

CFReadStreamRef

NSinputStream

CFRunLoopTimerRef

NSTimer

CFSetRef

NSSet

CFStringRef

Nsstring

CFTimeZoneRef

NSTimeZone

CFURLRef

NSURL

CFWriteStreamRef

NSOutputStream

Mac OS X v10.0

Note: Not all data types are toll-free brIDged,even though their names might suggest that they are. For example,102)">NSRunLoop is not toll-free brIDged to CFRunLoop,102)">NSBundle is not toll-free brIDged to CFBundle,and NSDateFormatter is not toll-free brIDged to CFDateFormatter.

(#add莫非桥接的均以Ref结尾)

总结

以上是内存溢出为你收集整理的Toll-Free Bridging全部内容,希望文章能够帮你解决Toll-Free Bridging所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1060499.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存