Re: [Newbie] reference count
Re: [Newbie] reference count
- Subject: Re: [Newbie] reference count
- From: publiclook <email@hidden>
- Date: Sun, 18 May 2003 11:21:44 -0400
In this case, the misunderstanding is that -init IS called for objects
instantiated within a nib file in certain situations. When an object
for which IB does not have code is instantiated in IB, IB actually
instantiates an instance of a place holder class. When the nib is
loaded by an application, the place holder is unarchived from the nib
and then allocates and initializes the actual instance. Hence, the new
instance's -init method is called and -initWithCoder: is not called.
This is in direct contracts to the situation in which an object for
which IB DOES have code is instantiated in IB. In that case, the real
object is instantiated inside IB and archived into the nib file with
-encodeWithCoder:. When the object is unarchived from a nib file, its
-initWithCoder: is called and not -init.
In both cases, -awakeFromNib is called.
When writing a class that may be instantiated in IB or programatically,
it is necessary to ensure correct initialization in every case. The
-awakeFromNib method is useful for both situations where the object is
instantiated in IB. The designated initializer is the only place to do
initialization for programatically instantiated instances:
Here is a table showing which initializers are called when:
Programmatic Know class in IB Unknown class in IB
Instantiation
------------------------------------------------------------
designated designated designated initializer
initializer initializer in application and then
only within IB and -awakeFromNib in
-initWithCoder: application.
in application.
Then -awakeFromNib
in application
Notice that the designated initializer is used in all cases. The only
thing that varies is when it is called. The -initWithCoder: method is
only used for instances of known classes in IB. The -awakeFromNib
method is only called if the instance was instantiated in IB.
Note: These rules apply to NSView subclasses also. In that case, the
-initWithFrame: method is the designated initializer instead of -init.
The "Cocoa Programming" example definitely has an error and is
incorrect or at least misleading in the text of Chapter 9.
The following implementation of -_myInitInstanceVariables corrects the
problem. It is not sufficient to skip initialization of the
_myOpenDocuments variable in any of the three scenarios shown in the
table above. To handle all cases, -_myInitInstanceVariables MUST be
called in -init and ALSO in one of -awakeFromNib or -initWithCoder:.
- (void)_myInitInstanceVariables
/*" Initialize instance variables: called by -init and -awakeFromNib.
"*/
{
NSRect screenVisibleFrame = [[NSScreen mainScreen]
visibleFrame];
// Create array of open documents
if(nil == _myOpenDocuments)
{
_myOpenDocuments = [[NSMutableArray allocWithZone:[self zone]]
init];
}
// Set initial cascade point
_myWindowCascadePoint = NSMakePoint(0, NSMaxY(screenVisibleFrame));
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.