Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mdeh

macrumors 6502
Original poster
Jan 3, 2009
345
2
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.

Code:
-(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 code
Code:
speechSynth = [[ 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

Moderator emeritus
Jun 15, 2000
7,958
7
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:
nib.png

This is documented here: The Nib Object Life Cycle

2) As far as I can tell, the code
Code:
speechSynth = [[ 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

macrumors 6502
Original poster
Jan 3, 2009
345
2


thank you...I guess it's time to get stuck into some of the documentation. Pretty daunting.
 

buckyballs

macrumors regular
Dec 22, 2006
176
97
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

macrumors 6502
Original poster
Jan 3, 2009
345
2
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.