Re: retain and don`t retain in accessor methods
Re: retain and don`t retain in accessor methods
- Subject: Re: retain and don`t retain in accessor methods
- From: Austin Ziegler <email@hidden>
- Date: Mon, 16 Nov 2009 08:18:21 -0500
On Sun, Nov 15, 2009 at 10:35 AM, Clark Cox <email@hidden> wrote:
> On Sat, Nov 14, 2009 at 11:35 PM, Ariel Feinerman <email@hidden> wrote:
>> 2009/11/15 Kyle Sluder <email@hidden>
>>> On Sat, Nov 14, 2009 at 2:35 PM, Ariel Feinerman <email@hidden>
>>> wrote:
>>> > I need two versions of -initWith ... and -set ... methods of custom timer
>>> > class (for example, to prevent the circle references);
>>> Do not use accessors in your initializers or -dealloc.
>> Hm, it means to do the same work two times, I think. For example if I change
>> the mechanism in the setting of instance variables, I must to change one in
>> difference inits method; can you say a reason?
> The reason often given (to which I don't personally subscribe) is that
> the accessor could be overridden in a subclass, and therefore do
> things that aren't appropriate (i.e. they could assume something about
> the already-dealloc'ed or not-yet-init'ed portions of the subclass).
Is that true anymore, though? I've been looking at a lot of sample
code lately, and it's very common practice to use "self.foo = ..." in
initializers, even when they're declared nonatomic. I know you said
you don't subscribe to it, but it seems to be an accepted practice
these days.
As to the original question, I'm not a Cocoa expert (far from it!),
but on general principles that sort of flag raises a code smell.
Sometimes it's unavoidable, but I'd either:
* use two different classes that have different ownership expectations
(one expects ownership and retains; the other does not and does not);
or
* have two different initializers with different names indicating the
different ownership (e.g., initWithTarget, initWithObserved). You may
have some code duplication here, but you could minimize some of that
with a "- (void)initTimer" method that sets time and calls begin.
-austin
--
Austin Ziegler • email@hidden • email@hidden
http://www.halostatue.ca/ • http://twitter.com/halostatue
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden