Core Foundation框架 (CoreFoundation.framework) 是一組C語言接口,它們為iOS應用程序提供基本數據管理和服務功能。下面列舉該框架支持進行管理的數據以及可提供的服務:
群體數據類型 (數組、集合等)
程序包
字符串管理
日期和時間管理
原始數據塊管理
偏好管理
URL及數據流操作
線程和RunLoop
端口和soket通訊
Core Foundation框架和Foundation框架緊密相關,它們為相同功能提供接口,但Foundation框架提供Objective-C接口。如果您將Foundation對象和Core Foundation類型摻雜使用,則可利用兩個框架之間的 “toll-free bridging”。所謂的Toll-free bridging是說您可以在某個框架的方法或函數同時使用Core Foundatio和Foundation 框架中的某些類型。很多數據類型支持這一特性,其中包括群體和字符串數據類型。每個框架的類和類型描述都會對某個對象是否為 toll-free bridged,應和什么對象橋接進行說明。
如需進一步信息,請閱讀Core Foundation 框架參考。
Objective-C指針與CoreFoundation指針之間的轉換】
ARC僅管理Objective-C指針(retain、release、autorelease),不管理CoreFoundation指針,CF指針由人工管理,手動的CFRetain和CFRelease來管理,注,CF中沒有autorelease。
CocoaFoundation指針與CoreFoundation指針轉換,需要考慮的是所指向對象所有權的歸屬。ARC提供了3個修飾符來管理。
1. __bridge,什么也不做,僅僅是轉換。此種情況下:
i). 從Cocoa轉換到Core,需要人工CFRetain,否則,Cocoa指針釋放后, 傳出去的指針則無效。
ii). 從Core轉換到Cocoa,需要人工CFRelease,否則,Cocoa指針釋放后,對象引用計數仍為1,不會被銷毀。
2. __bridge_retained,轉換后自動調用CFRetain,即幫助自動解決上述i的情形。
2. __bridge_transfer,轉換后自動調用CFRelease,即幫助自動解決上述ii的情形。
英文解釋(更全面):
What's the point of them both existing? There are a few reasons.
If you want to provide a C API, like the Carbon API, and you need things like arrays and dictionaries of referenced-counted objects, you want a library like Core Foundation (which providesCFArray), and of course it needs to have a C API.
If you want to write libraries for third-parties to use on Windows (for example), you need to provide a C API.
If you want to write a low-level library, say for interfacing with your operating system's kernel, and you don't want the overhead of Objective-C messaging, you need a C API.
So those are good reasons for having Core Foundation, a pure C library.
But if you want to provide a higher-level, more pleasant API in Objective-C, you want Objective-C objects that represent arrays, dictionaries, reference-counted objects, and so on. So you need Foundation, which is an Objective-C library.
When should you use one or the other? Generally, you should use the Objective-C classes (e.g.NSArray) whenever you can, because the Objective-C interface is more pleasant to use:myArray.count(or[myArray count]) is easier to read and write thanCFArrayGetCount(myArray). You should use the Core Foundation API only when you really need to: when you're on a platform that doesn't have Objective-C, or when you need features that the Core Foundation API provides but the Objective-C objects lack. For example, you can specify callbacks when creating aCFArrayor aCFDictionarythat let you store non-reference-counted objects. TheNSArrayandNSDictionaryclasses don't let you do that - they always assume you are storing reference-counted objects.
Are the CF objects just legacy objects? Not at all. In fact, Nextstep existed for years with just the Objective-C Foundation library and no (public) Core Foundation library. When Apple needed to support both the Carbon API and the Cocoa API on top of the same lower-level operating system facilities, they created (or made public) Core Foundation to support both.
Incidentally, some of Core Foundation is open source. You can find the open source part of it for Mac OS X 10.10.5 here:https://opensource.apple.com/source/CF/CF-1153.18/. I have found the source code ofCFRunLoopandCFStreamto be very informative.
參考資料:
http://stackoverflow.com/questions/9353804/cf-objects-vs-ns-objects
http://m.blog.csdn.net/article/details?id=8271867