Starting to play with the Address Book on iPhone. One interesting caveat about the Address Book API is it written in C and makes use of Core Foundation (rather than Foundation).
Foundation vs. Core Foundation
Core Foundation provides the basis for Foundation. In fact, Core Foundation provides the basis for all Objective-C types under the hood. From the Apple Documentation we have the following table that defines all types that are shared:
| Core Foundation type | Foundation class | Availability |
|---|---|---|
CFArrayRef |
NSArray |
Mac OS X v10.0 |
CFAttributedStringRef |
NSAttributedString |
Mac OS X v10.4 |
CFCalendarRef |
NSCalendar |
Mac OS X v10.4 |
CFCharacterSetRef |
NSCharacterSet |
Mac OS X v10.0 |
CFDataRef |
NSData |
Mac OS X v10.0 |
CFDateRef |
NSDate |
Mac OS X v10.0 |
CFDictionaryRef |
NSDictionary |
Mac OS X v10.0 |
CFErrorRef |
NSError |
Mac OS X v10.5 |
CFLocaleRef |
NSLocale |
Mac OS X v10.4 |
CFMutableArrayRef |
NSMutableArray |
Mac OS X v10.0 |
CFMutableAttributedStringRef |
NSMutableAttributedString |
Mac OS X v10.4 |
CFMutableCharacterSetRef |
NSMutableCharacterSet |
Mac OS X v10.0 |
CFMutableDataRef |
NSMutableData |
Mac OS X v10.0 |
CFMutableDictionaryRef |
NSMutableDictionary |
Mac OS X v10.0 |
CFMutableSetRef |
NSMutableSet |
Mac OS X v10.0 |
CFMutableStringRef |
NSMutableString |
Mac OS X v10.0 |
CFNumberRef |
NSNumber |
Mac OS X v10.0 |
CFReadStreamRef |
NSInputStream |
Mac OS X v10.0 |
CFRunLoopTimerRef |
NSTimer |
Mac OS X v10.0 |
CFSetRef |
NSSet |
Mac OS X v10.0 |
CFStringRef |
NSString |
Mac OS X v10.0 |
CFTimeZoneRef |
NSTimeZone |
Mac OS X v10.0 |
CFURLRef |
NSURL |
Mac OS X v10.0 |
CFWriteStreamRef |
NSOutputStream |
Mac OS X v10.0 |
Because these types are actually the same, you can convert back and forth.
Toll Free Bridging
So, back to Address Book. It is written in C using Core Foundation (meaning it returns C types). This means that your Objective-C iPhone code is going to have a mismash of C types when using some of the System level APIs (almost all System level APIs are written in C to reduce memory footprint).
You lose some of the features of Objective-C (you are really just working with C at this point). So, you need to make sure you check for NULL before using a returned type or you will crash. No message passing to “nil” here.
CFStringRef string = StringReturnedHere();
if(string != NULL) {
PlayWithString(string);
}
However, not all is lost here, and you don’t have to go back to the scary C world if you don’t want to. You can, in fact, convert all these types into Object-C Foundation classes and operate them as you would if it was returned by an Objective-C API.
NSString string = (NSString *)StringReturnedHere(); [string doSomethingCool];
Toll Free Bridging allows you to type cast from one Core Foundation type to a Foundation type and vice versa. This allows you to work with Core Foundation APIs directly but treat the results as Foundation.