PDA

View Full Version : init method not called




hervey
Nov 2, 2009, 12:56 PM
The init method is NOT called on the second snippet of code, but is called on the first snippet. I don't understand.

@implementation testAERAppDelegate
@synthesize window;

-(id)init {
[super init];
NSLog(@"init"); ...
return self;
}



@implementation RootViewController
@synthesize pickerView,dateFormatter;

-(id)initWithCoder {
[super init];
NSLog(@"init");
return self;
}



jeremy.king
Nov 2, 2009, 01:03 PM
-(id)initWithCoder


Your method isn't called init

xStep
Nov 2, 2009, 02:25 PM
initWithCoder is used to unarchive content from a file.

You'll want to read "Archives and Serializations Programming Guide for Cocoa (http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Archiving/Archiving.html)" for more on that.

hervey
Nov 2, 2009, 09:11 PM
My Bad! I copied over other code I was trying out. The correct posting of the code that's NOT working is:

@implementation RootViewController
@synthesize pickerView,dateFormatter;

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


Sorry for the confusion. Thanks for your help.

neoserver
Nov 2, 2009, 09:22 PM
How are you instantiating this class? Can you post the code where you are using the class?

Is this RootViewController from inside a XIB/NIB? If so, it technically doesn't get init'ed when the app is started, but rather gets unarchived from the XIB. When this happens, the awakeFromNib method is called.

Basically the rule of thumb, is if the class is being instantiated from code, init is called, but if the class is being instantiated from Interface Builder, then you want awakeFromNib.

hervey
Nov 4, 2009, 03:18 PM
One from xib, one from code. Thank you for helping clarify this basic element, and the clear manner you made it clear for me. I have been studying Cocoa since March, and this basic element had escaped me.

Cheers.