 |
 |
Initializing objects at program launch
|
 |
|
 |
|
Junior Member
Join Date: Dec 2000
Location: Houston, TX, USA
Status:
Offline
|
|
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
|
|
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
|
|
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
|
|
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
|
|
I appreciate your clear explanation, Dalgo, and I commend you for it.
(My favorite Al Gore line(I'll take you there))
------------------
Think Different.
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|