A primary characteristic of Core Foundation objects is that they’re based on an opaque (or private) type; it is thus difficult to inspect the internal data of an object directly. Base Services, however, provide two functions with which you can inspect Core Foundation objects. These functions return descriptions of an object and of the object’s type.
To find out the contents of a Core Foundation object, call the CFCopyDescription
function on that object and then print the character sequence “contained” in the referred-to string object:
Listing 1 Using CFCopyDescription
void describe255(CFTypeRef tested) { |
char buffer[256]; |
CFIndex got; |
CFStringRef description = CFCopyDescription(tested); |
CFStringGetBytes(description, |
CFRangeMake(0, CFStringGetLength(description)), |
CFStringGetSystemEncoding(), '?', TRUE, buffer, 255, &got); |
buffer[got] = (char)0; |
fprintf(stdout, "%s", buffer); |
CFRelease(description); |
} |
This example shows just one approach for printing a description. You could use CFString functions other than CFStringGetBytes
to get the actual string.
To determine the type of an “unknown” object, obtain its type ID with the CFGetTypeID
function and compare that value with known type IDs until you find a match. You obtain an object’s type ID with the CFGetTypeID
function. Each opaque type also defines a function of the form CF
TypeGetTypeID
(for example, CFArrayGetTypeID
); this function returns the type ID for that type. Therefore, you can test whether a CFType object is a member of a specific opaque type as in:
CFTypeID type = CFGetTypeID(anObject); |
if (CFArrayGetTypeID() == type) |
printf("anObject is an array."); |
else |
printf("anObject is NOT an array."); |
To display information about the type of a Core Foundation object in the debugger, use the CFGetTypeID
function to get its type ID, then pass that value to the CFCopyTypeIDDescription
function:
/* aCFObject is any Core Foundation object */ |
CFStringRef descrip = CFCopyTypeIDDescription(CFGetTypeID(aCFObject)); |
Note: String Services include two functions, both declared in CFString.h
, that you can call in supported debuggers to print descriptions of Core Foundation objects: CFShow
and CFShowStr
.
Important: The CFCopyDescription
and the CFCopyTypeIDDescription
functions are for debugging only. Because the information in the descriptions and their format are subject to change, do not create dependencies on them in your code.
© 2003, 2005 Apple Computer, Inc. All Rights Reserved. (Last updated: 2005-08-11)