Re: Double Initialize - is that how it should be?
Re: Double Initialize - is that how it should be?
- Subject: Re: Double Initialize - is that how it should be?
- From: WT <email@hidden>
- Date: Tue, 17 Mar 2009 03:46:03 +0100
I'm pretty much a Cocoa noob, so I may be wrong, but my understanding
is that objects instantiated in nib files should be initialized in the
-awakeFromNib method of their classes.
In your specific example, the nib instantiates an object of the
AppDelegate class by sending that class the nested message
[[AppDelegate alloc] init] (note that the -init message is the bread-
and-butter one, with no arguments). Once all objects in the nib have
been instantiated, they all receive the -awakeFromNib message and it's
in the implementation of that message that you should perform any
initialization specific to the object in question.
Hope that helps.
Wagner
On Mar 17, 2009, at 3:21 AM, Steve Cronin wrote:
Folks;
I use an object in my application called 'appDelegate'; it's a
subclass of NSObject.
I instantiate this object in the main Nib file
This object has IBActions for the main menu and since it is -app
delegate it is a kind on central dispatcher.
My question is this:
The +initialize is run twice when the app loads:
2009-03-16 20:21:42.690 XYZ[6676:813] *[AppDelegate initialize]
2009-03-16 20:21:42.781 XYZ[6676:813] *[NSKVONotifying_AppDelegate
initialize]
Once for the object itself and then again as the instance in the
Nib. That's how I read this. Is that wrong?
So I wonder if this is wasteful. I don't really want to run
initialize twice.
It kind of torques my notion of a class that +initialize is run
twice but I try and just play along....
I don't see how to use this IBActions and IBOutlets without
instantiating.
Is the correct thing to do here break this object into smaller
objects?
Thanks for any clarifying comments
Steve
+initialize {
//check minimum system OS - so I can gracefully tell the user what
is deal is and how they should upgrade - if not min then die
//check whether or not the app has access to the ~/Library/
Application Support - if not then die
//test for keyDown - to flip the logging level
}
- init {
//set this object as app delegate
[[NSApplication sharedApplication] setDelegate:self];
//NSFormatters and NSValueTransformers which must be done here so
awakeFromNib binding transformers are set
[self setupApplicationBehavior];
//hang onto the prefPath
prefFile =
[[[[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
NSUserDomainMask, YES) objectAtIndex:0]
stringByAppendingPathComponent:@"Preferences"]
stringByAppendingPathComponent:[[[[NSBundle mainBundle]
infoDictionary] valueForKey:@"CFBundleIdentifier"]
stringByAppendingString:@".plist"]] retain];
//determine if this is the first time prefs have been used
firstPrefUse = (![[NSFileManager defaultManager]
fileExistsAtPath:prefFile]);
//load userPrefs here so they can be used by -awakeFromNib
userDefaults = [[NSUserDefaults standardUserDefaults] retain];
[self registerFactoryDefaults];
return self;
)
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden