1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Who is calling init?

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

  1. macrumors 6502

    #1
    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.
     
  2. Moderator emeritus

    kainjow

    #2
    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

    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. macrumors 6502

    #3


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

    #4
    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. macrumors 6502

    #5

    Thanks
     

Share This Page