Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
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?
 

Nutter

macrumors 6502
Mar 31, 2005
432
0
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.

Example:

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:

Code:
Str255 pascalString = "\pHello, world!";
CFStringRef cfString = CFStringCreateWithPascalString(kCFAllocatorDefault, pascalString, kCFStringEncodingUTF8);
NSLog(@"%@", (NSString *)cfString);
CFRelease(cfString);

This works because many Foundation classes are toll-free bridged to Core Foundation types.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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.
 

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
I see. Thanks a lot. Maybe I will take a look at it to see what's available... I may need it one day.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.