20090527

awakeFromNib

What does awakeFromNib actually mean? Using the Cocoa principle of "it's probably what it sounds like," a naive programmer would assume that this method is invoked when the program awakes, i.e. receives focus. Alas, this is far from accurate. Someone seems to think that an object created via nib file instead of programmatically requires extra code, and that's possibly true; but "awake from nib?" Where's the waking up?

A nib file is an xml archive that in a sense dehydrates Cocoa objects for runtime reconstitution. However, it can require further tweakage, so every thus-created object receives the awakeFromNib message. It can be useful, for instance, in establishing connections; the app delegate manager receives awakeFromNib before applicationDidFinishLaunching, so I use it to create some connections such as (instance variable) dockTile = [NSApp dockTile]; which I can then refer to everywhere else in my class, knowing that it will have a valid value immediately after the object is instantiated, before any other code can run.


- (void)awakeFromNib
{
  dockTile = [NSApp dockTile];
}

20090518

Cocoa app delegate

Programming with the Cocoa APIs has been a very strange experience for me. My first experience was on the iPhone with Cocoa Touch, and right when I had sort-of figured that out, I had to switch to OS X development.
Yesterday, I figured out how to badge dock icons. I found the appropriate documentation, but I still didn't understand, so I downloaded the sample project. Now I had proof of existance, but couldn't see how it was working. Finally, I opened the nib, and saw that the AppDelegate class was connected via Interface Builder as the NSApplication's delegate, and thus its delegate functions were called as appropriate.
I cannot figure out how to achieve this same goal programmatically, so I copied their method, and it does indeed work.

EDIT: the following is untested, but seems like it should work:
Create an instance of the delegate class, and in its -awakeFromNib method, insert the follwing code:
[NSApp setDelegate:self];

the end.