Home > Articles > Home & Office Computing > Mac OS X

  • Print
  • + Share This
This chapter is from the book

4.2 Core Foundation Types

The Core Foundation (CF) library contains a set of C opaque types that have a similar interface to a number of Cocoa Foundation objects. This similarity is not accidental. The aim of Core Foundation was to produce a rich common set of fundamental types that both Cocoa and Carbon applications could use. This is no longer important, since Carbon did not make the 64-bit switch, but Core Foundation is still used in a lot of low-level parts of OS X, such as Launchd.

Although C does not have a notion of inheritance on types, Core Foundation types are built into a hierarchy. At the root is CFType, which implements basic memory management for CF types. Just as Cocoa objects are reference counted with -retain and -release messages, Core Foundation types are reference counted by calling the CFRetain() and CFRelease() functions with them as an argument.

Many of the Core Foundation types use the toll-free bridging mechanism to interoperate with their Cocoa equivalents. The first field in any CF structure is an isa pointer, just as with an Objective-C object. Unlike Cocoa objects, however, this value is always between 0 and 216, a region of memory where no Objective-C classes will be. When you send a message to a CF object, the message send function will use a special case for class pointers in this range.

Similarly, when you call a Core Foundation function with a Cocoa object, it will test that the isa pointer is greater than 0xFFFF and, if it is, then call the Objective-C runtime functions for method dispatch, bouncing the call back to Objective-C. This allows you to use the Core Foundation types and Cocoa objects interchangeably.

A lot of the Cocoa Foundation objects have Core Foundation analogues. The most common is probably CFString, the equivalent of Cocoa's NSString. In fact, both NSString and NSMutableString are class clusters on Cocoa, meaning that their instances may not really be versions of that class. Under the hood, all three types are implemented by the NSCFString type. This is true for a lot of class clusters in Cocoa.

  • + Share This
  • 🔖 Save To Your Account