Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Initializing objects at program launch

Initializing objects at program launch
Thread Tools
Junior Member
Join Date: Dec 2000
Location: Houston, TX, USA
Status: Offline
Reply With Quote
Jan 6, 2001, 02:06 PM
 
Hello!
When i create an application, what the proper way to initialize data structure objects and such at program launch. Should I create the object that stores my data in IB and instantiate it there? Create the class in PB and create an instance of that class from main.m? Or should i just work with Class objects and static instance variables? What is the proper way of accomplishing this task in OOP/Cocoa/ObjC/Java or whereever? Thanks for your help!

Mike
     
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status: Offline
Reply With Quote
Jan 7, 2001, 06:33 AM
 
First of all, when objects are allocated in Objective C they use calloc so all of your variables are zeroed, so if all you were going to do was zero stuff then you don't need to bother. Anyway, if you want to initialize variables when you make new objects, then in the class implementation add:

- (id)init
{
if (self = [super init])
{
// Whatever you want to initialize, so it here.
{
return self;
}

You don't need to prototype that in the header. If you wish to initialize variables when your program starts and you don't want to do the same when you make new objects as your program is running then, if your classes are instantiated in Interface Builder, you can add this to the class implementation:

- (void)awakeFromNib
{
// Do whatever you want here.
}

No need to prototype it. It's called automatically when the nib file is first read. If you have a program with multiple nib files then the awakeFromNib methods in it's classes are only called as those files are used.
     
Junior Member
Join Date: Dec 2000
Location: Houston, TX, USA
Status: Offline
Reply With Quote
Jan 7, 2001, 06:43 PM
 
So i gather from your reply that the standard procedure in Cocoa dev is to instantiate any custom classes in Interface builder, and handle instance variable initialization in the awakeFromNIB method? Is this correct? If so, it seems that Interface Builder plays a bigger role in developement than i originally thought. Is there anyway i can hand code anything IB does automatically? Well, thats it for now. Thanks for replying.

Mike
     
Mac Enthusiast
Join Date: Feb 2000
Location: Storrs,Connecticut, USA
Status: Offline
Reply With Quote
Jan 8, 2001, 06:03 AM
 
You don't need to use interface builder. If you are going to dynamically create and destroy objects it's easier to just initialize them using init. To allocate and initialize an object you can do this:

pointertoobject = [[ClassName alloc] init];

Not all objects have to be made in interface builder.
     
Mac Elite
Join Date: Jan 2001
Location: New York
Status: Offline
Reply With Quote
Jan 15, 2001, 08:34 PM
 
I appreciate your clear explanation, Dalgo, and I commend you for it.

(My favorite Al Gore line(I'll take you there))

------------------
Think Different.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 11:24 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2