So I have finally had time to code to my heart’s content over the holiday break. I thought that I would share some of my thoughts and experiences with programming for Leopard.
Fast Enumeration
Cocoa now implements a foreach() type of loop, which cleans up code tremendously. Iterating over an array or dictionary is pretty common stuff and being able to condense code from:
NSEnumerator *e = [myArray objectEnumerator];
id item;
while ( item = [e nextObject] ) {
NSLog(@”%@”, [item printSomething]);
}
to a more elegant:
id item;
for ( item in myArray ) {
NSlog(@”%@”, [item printSomething]);
}
Properties
Properties are a wonderful addition to Cocoa and essentially eliminate writing basic accessor methods for your objects (setters and getters).
@interface Song : NSObject {
NSString* title;
Artist* artist;
NSDate* dateAdded;
}
@property(readwrite, copy)NSString* title;
@property(readwrite, copy)Artist* artist;
@property(readwrite, copy)NSDate* dateAdded;
@implementation Song @synthesize title, artist, dateAdded;
The ObjC 2.0 compiler gives you extreme flexibility with properties, beyond just the basic use of accessors here. You can specify a property to be readonly instead of readwrite, or to retain or assign instead of copy.
@dynamic title; -(NSString* )title { }
The above would require you to implement the method yourself, letting the compiler know you are handling that.
NSTreeNode
Not a feature of the ObjC language, but certainly a nice addition to help using NSTreeController. NSTreeNode is a wrapper object which aids in creating trees. Just create a NSTreeNode and add other NSTreeNode objects to the -mutableChildNodes array and you are on your way to a tree. Binding this tree to a NSTreeController is relatively simple as well. Of note, remember that if you are using NSOutlineView delegate methods, you receive a NSTreeNode object now, so you must use -representedObject on “id item”.
A further note, when using the “selection” controller key on a NSTreeController, the controller returns an array of NSTreeControllerProxyObjects. Either call -self on the NSTreeControllerProxyObject or when using bindings (binding a second NSTreeController to the “selection” of the first one) remember to use “selection” with model key path of “self”. I don’t believe this is documented anywhere, but using “self” clears up a whole world of heartache and gives you the actual NSTreeNode instead of the NSTreeControllerProxyObject.
Interface Builder
It took some getting used to, but recall that you have to drop a NSObject (blue cube) from the IB Library and set the custom class to initiate an object in IB 3. The old method of “Initiate Class” or whatever from the menu isn’t available. Also remember that IB Palettes don’t work with IB 3, you will need to find (or create) IB Plugins.
Core Animation
Recall that the -orderIn and -orderOut options in IB are for adding and removing subviews to the selected view in IB (-setWantsLayer:YES). So NSTabView will not magically transition views in and out unless you are programatically creating new tabs and such.
That was just a quick list of the headache I endured over the last few weeks adjusting to Leopard and mainly spending lots of time with trees in Cocoa.
I’d love to hear your thoughts about ObjC 2.0 additions, whether it’s garbage collection or something more minute!

Chat via AIM


