CoreFoundation: What am I missing?

Discussion in 'Mac Programming' started by Soulstorm, Sep 3, 2007.

  1. Soulstorm macrumors 68000


    Feb 1, 2005
    I program with Cocoa on OS X for some time now, and now and then I stumble across some guy that mentions solutions in Cocoa applications using CoreFoundation.

    What exactly are the strengths and weaknesses of CoreFoundation? If I begin to learn it, will it be a valuable asset to my Cocoa projects?
  2. Nutter macrumors 6502

    Mar 31, 2005
    London, England
    Core Foundation is the procedural C API upon which much of Cocoa's Foundation framework is (now) based.

    Core Foundation is somewhat lower level than Cocoa, so in some cases it offers features that aren't available in Cocoa - in this sense, yes, knowledge of Core Foundation can be valuable. However, systematically learning the whole API is by no means necessary.


    Let's say you need to convert a Pascal string to an NSString. How can you do that? There's no method on NSString for this, but you can use Core Foundation:

    Str255 pascalString = "\pHello, world!";
    CFStringRef cfString = CFStringCreateWithPascalString(kCFAllocatorDefault, pascalString, kCFStringEncodingUTF8);
    NSLog(@"%@", (NSString *)cfString);
    This works because many Foundation classes are toll-free bridged to Core Foundation types.
  3. kainjow Moderator emeritus


    Jun 15, 2000
    One example of how knowing CoreFoundation can be helpful when doing Cocoa development is creating a URL that has characters properly escaped:

    If you're developing for 10.4+, you can use NSString's method stringByAddingPercentEscapesUsingEncoding: but if you need to develop for any other previous version, you can use CoreFoundation's CFURLCreateStringByAddingPercentEscapes function.
  4. Soulstorm thread starter macrumors 68000


    Feb 1, 2005
    I see. Thanks a lot. Maybe I will take a look at it to see what's available... I may need it one day.

Share This Page