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 > Coacoa's equivalent of a constructor

Coacoa's equivalent of a constructor
Thread Tools
Fresh-Faced Recruit
Join Date: Feb 2001
Location: Canada
Status: Offline
Reply With Quote
Feb 11, 2001, 06:05 PM
 
When programming in Coacoa, what is the Java equivalent of a constructor. So far I'm doing all initializations of instance variables in the "-(void)awakeFromNib" method. Is this usually the correct place to do inits? Or should I make my own constructor. If so, how would one approach this?

     
ali
Forum Regular
Join Date: Sep 2000
Status: Offline
Reply With Quote
Feb 11, 2001, 07:13 PM
 
Cocoa has "init" methods, which are usually called right after allocation to initialize objects, for instance:

myString = [[NSString alloc] initWithCharacters:chars length:len];

Typically you nest the alloc and initXXX calls, as init might change the object returned by alloc.

Each class can have as many initXXX methods as it wants, but typically a very small number is known as "designated" initializers, which is what all the other init methods end up calling. The designated initializers then in turn are the ones call init methods in the superclass, allowing all superclasses to initialize parts of the object they care about. (Eventually it all boils down to NSObject's init method.)


Subclassers which want to have init methods should normally override the designated initializers, so that other init methods automatically do the right thing (by going through the subclass method).

All this is not a language requirement, but it's a good policy, and one that most Cocoa classes follow.


As far as where to initialize instance vars. awakeFromNib is reasonable for objects created from nib, but it won't get called for objects created programmatically. If you want to allow programmatic creation of your object, you would usually make sure your init methods (preferably just the designated initializers) go through the same code path awakeFromNib does to initialize them.

One more note: Objects coming from nib and other object archives first get alloc, then initWithCoder:, then awakeFromNib. initWithCoder: is usually where ivars are read from the archive and initialized. awakeFromNib is best for doing somewhat deferred initializations, ones which make sure that all of the outlets and such are also initialized. (At initWithCoder: time, other objects being loaded from the nib might not yet be initialized themselves...)

I hope I didn't confuse you too much,
Ali


[This message has been edited by ali (edited 02-11-2001).]
     
   
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 12:22 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