SpeakLineAppDelegate with -init method overwrite



Good book so far. Had a couple of questions though.

In the SpeakLine example program, you overwrote the -init method of the SpeakLineAppDelegate class. When does this get called? My assumption is the second an action is sent to it. It is just weird not to see it written out (e.g. SpeakLineAppDelegate *someDelegate = [[SpeakLineAppDelegate alloc]init]:wink: anywhere.

Also, I did the example on my own and initilized the _speechSynth object in the -(void)applicationdidfinishlaunching: method. This seemed to work just fine. Am I overlooking something by not overwriting the init?



Good questions. Our SpeakLineAppDelegate instance is created by the xib/nib file, so -init is called as soon as the MainMenu nib is unarchived. Note how the project template put a blue object in the xib file (“Speak Line App Delegate”) – any object in the interface builder dock (except the placeholders at the top, such as File’s Owner and First Responder) is instantiated by the nib file.

Personally, I avoid creating controller classes this way, as I prefer the more explicit style. Oftentimes, in my apps, the app delegate will be the only controller class created in this fashion.

In the SpeakLine app it’s fine to create the speech synth in either place. If, however, you had tried to initialize _voices in -applicationDidFinishLaunching:, you would find that the table is not populated. The reason is that the table loads its contents for the first time before -applicationDidFinishLaunching:. So -init is a better choice. -awakeFromNib would work as well.

So because -applicationDidFinishLaunching: is called after -init and -awakeFromNib, each is appropriate for certain types of initialization, although the lines are blurry in many cases. If you have any setup for your app delegate that other objects are going to be depending on, I’d suggest doing those steps in -init. -applicationDidFinishLaunching: is a good place to do any final steps in the app startup sequence, such as displaying a first run window.




Thanks for the quick reply, and good to know. As soon as I got done typing my questions on this BB, I got to the part about initializing _voices and just as you said it didn’t work. Went back and used -init.