Re: semantics of init
Re: semantics of init
- Subject: Re: semantics of init
- From: Daniel Zitter <email@hidden>
- Date: Wed, 4 Jun 2003 07:15:21 -0700
Date: Wed, 4 Jun 2003 10:34:27 +0200 (MEST)
From: Christian Brunschen <email@hidden>
To: Marcel Weiher <email@hidden>
cc: email@hidden
Subject: Re: semantics of init
On Wed, 4 Jun 2003, Marcel Weiher wrote:
Anyway, the pattern I use is the following:
-init
{
self=[super init];
[self setDict:[NSMutableDictionary dictionary]];
[self setArray:[NSMutableArray array];
return self;
}
So I use my accessors to avoid direct instance variable access. This
protects me from self being nil without the test, and it even protects
me from a different type of object being returned. (If the object
being returned isn't compatible in the message-interface, then it is
just broken).
Messaging is good.
Marcel
Add in particular, accessor methods are good. They should always be
used,
even, or should I say _especially_, in -init... and -dealloc.
Specifically, rather than
- dealloc {
[dict release];
[array release];
[super dealloc];
}
I would use
- dealloc {
[self setDict:nil];
[self setArray:nil];
[super dealloc];
}
for much the same reasons Marcel is citing in the case of the -init...
method above.
// Christian Brunschen
Christian,
While I agree the practice of using setter methods to release retained
objects can be advantageous, I don't follow the rationale as stated.
The reasons Marcel gives for using accessors in -init don't apply to
the case of -dealloc because your instance certainly isa your class if
your dealloc method is getting called.
Dan
_______________________________________________
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.