PDA

View Full Version : Who is calling init?




mdeh
Mar 8, 2009, 10:34 PM
Some of these initial queries will be pretty dumb, so I apologize advance.
The documentation says a lot about everything, except what I want to know :)

This is from Hillegas, creating an App called SpeechSynthesizer.

Setup. Window, an NSTextField, 2 NSButtons. The text in the field is spoken when the (Button) "Speak" is pushed.

An instance of AppController ( a class created for the app) has been placed in the .nib file and all outlets, methods have been connected correctly, as the App runs as it should.

Here are my queries.

In AppController.m this init is found.

-(id) init
{
[super init];
NSLog(@"init");

speechSynth = [[ NSSpeechSynthesizer alloc] initWithVoice: nil];
return self;
}

1) Who calls this init?
In plain Obj-C, I would call this init with something like

MyClass *myC = [[ MyClass alloc..] initWithWhatever];

My **guess** is that all nib objects get sent an init on launch, but I would like to know that for sure.


2) As far as I can tell, the codespeechSynth = [[ NSSpeechSynthesizer alloc] initWithVoice: nil]; is not released. Is this intentional, or just an early explanation and all will be later explained?

Thanks as always.



kainjow
Mar 9, 2009, 09:30 AM
1) Who calls this init?
The nib loading process calls this. If you set a breakpoint at your init method, you can see what calls it:
161532

This is documented here: The Nib Object Life Cycle (http://developer.apple.com/DOCUMENTATION/Cocoa/Conceptual/LoadingResources/CocoaNibs/chapter_3_section_4.html)

2) As far as I can tell, the codespeechSynth = [[ NSSpeechSynthesizer alloc] initWithVoice: nil]; is not released. Is this intentional, or just an early explanation and all will be later explained?
I don't have access to the full code, but most likely it is released in the dealloc method. If not, it should be. However if it's in an object like AppController above, or something that is around for the entire life of the program, it's not a big deal because the OS will release it when the app exits. But you should still get in the habit of releasing your objects.

mdeh
Mar 9, 2009, 04:33 PM
thank you...I guess it's time to get stuck into some of the documentation. Pretty daunting.

buckyballs
Mar 13, 2009, 05:25 AM
My **guess** is that all nib objects get sent an init on launch, but I would like to know that for sure.

They do all get called init, but objects in a nib file are not all initialised at once. Therefore in this method you cannot rely on the other objects in the nib file being available.

Once all the objects in a nib file have been initialised, the nib loader will trigger all objects awakeFromNib method simultaneously.

If your code requires other objects in the nib file put your code in the awakeFromNib method

mdeh
Mar 15, 2009, 09:19 AM
They do all get called init, but objects in a nib file are not all initialised at once. Therefore in this method you cannot rely on the other objects in the nib file being available.

Once all the objects in a nib file have been initialised, the nib loader will trigger all objects awakeFromNib method simultaneously.

If your code requires other objects in the nib file put your code in the awakeFromNib method


Thanks