My first Cocoa steps
Posted by Anil Madhavapeddy Sat, 15 Jul 2006 02:45:00 GMT
Ever since I got my first Powerbook back in 2000, I've been meaning to learn Cocoa and Carbon to hack on MacOS X GUIs. On the flight over to San Francisco, I finally found the uninterrupted time to knock up a simple GUI interface to my gallery meta-data files. Overall, the experience was pretty positive. It's definitely more satisfying than my experiences with Windows and UNIX GUI programming, mainly because the result is so pretty!
Objective-C has a really nice dynamic message dispatch mechanism, used to good effect by Cocoa. The autorelease mechanism plays well with C memory management to provide a reference-counted interface which is fairly well abstracted. My application still leaks memory like a sieve though; I miss OCaml!
Cocoa provides a huge number of ways to do the same thing. Bindings are the fancy new thing, MVC is the older "paradigm", and of course Carbon predates all of this. The documentation is a bit of a mess for a beginner to the framework, as crucial facts on how to get simple stuff done are scattered across a myriad of documents. Thanks Google.
Tiger has some funky new Cocoa controls. Check out the use of NSTokenField on the screenshot which lets the tag entry field auto-complete just like Mail does!
The APIs can be unfortunately verbose at times, as the following code snippet which does a little bit of simple string manipulation shows.
NSArray *a = [tag componentsSeparatedByString:@" "];
iter2 = [a objectEnumerator];
while (s = [iter2 nextObject]) {
NSString *e = [s stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];
if (![e isEqual:@""])
[tags addObject:[e retain]];
}I'm quite looking forward to doing more work on this simple application now that the basics are mastered. Hmm... preferences panes next, then a bit of Cocoa bindings, and perhaps I feel OCamlCocoa coming on...
Yeah, objectEnumerators can be a pain sometimes, especially when you're used to functional constructs like map and filter. In NSArray and NSMutableArray there's a way to map a function on to each element of the array which is like a halfway point between the two.
I actually love the long function names, no more guessing what atoi and ioctl means!
Yeah, and NSPredicate seems like an interesting way to do filtering/search stuff.