IOS之Core Foundation框架和Cocoa Foun
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