Who is calling init?

Discussion in 'Mac Programming' started by mdeh, Mar 8, 2009.

  1. macrumors 6502

    Jan 3, 2009
    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];
    	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
    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.
  2. Moderator emeritus


    Jun 15, 2000
    The nib loading process calls this. If you set a breakpoint at your init method, you can see what calls it:

    This is documented here: The Nib Object Life Cycle

    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.
  3. thread starter macrumors 6502

    Jan 3, 2009

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

    Dec 22, 2006
    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
  5. thread starter macrumors 6502

    Jan 3, 2009


Share This Page