Re: Accessor method styles
Re: Accessor method styles
- Subject: Re: Accessor method styles
- From: Alberto Ricart <email@hidden>
- Date: Tue, 29 Aug 2006 10:51:40 -0500
On Aug 29, 2006, at 9:53 AM, Andrew Merenbach wrote:
> Hi, all. I've seen threads where this has been beaten to death,
> but I am still left with major questions. I have seen several
> kinds of accessor/mutator methods:
>
> - (void)setFoo:(AMFoo *)aFoo {
> [aFoo retain];
> [myFoo release];
> myFoo = aFoo;
> }
This is the standard idiom.
>
> - (void)setFoo:(AMFoo *)aFoo {
> if (aFoo != myFoo) {
> [myFoo release];
> myFoo = [aFoo retain];
> }
> }
This one is a better version of the standard idiom. It doesn't do any
work if the two references are equal. [albeit if you get set the same
reference over and over, you probably have something wrong elsewhere].
>
> - (void)setFoo:(AMFoo *)aFoo {
> id oldFoo = myFoo;
> myFoo = [aFoo retain];
> [oldFoo release];
> }
This one is a silly version of #1.
>
> - (void)setFoo:(AMFoo *)aFoo {
> [myFoo autorelease];
> myFoo = [aFoo retain];
> }
This one is also silly. Autorelease is more expensive than release.
Autorelease in most cases should be reserved for where you cannot
hold the reference, or when you are returning something someone else
should retain- #1 would be better, #2 even better.
Also, in *MOST* cases, if AMFoo was a mutable class, you really would
want to implement copy to return the non-mutable type. Copy should be
implemented to test if the type is mutable and if it is, return an
the immutable version. If it isn't mutable, it should call retain,
and return the same reference, in those cases you may see:
- (void) setFoo:(AMFoo*)aFoo
{
[myFoo autorelease];
myFoo = [aFoo copy];
}
>
> My question is not precisely which is "better," as my understanding
> is that they *all* (or at least the first two and the last one)
> have their merits, speed considerations, etc.
>
> What I want to know, as an intermediate Cocoa programmer--and what
> I have been unable (at least with my own searching) to find on the
> archives--is what are the *advantage* and *disadvantages* of each?
> I know some basics, such as that the last one can apparently make
> debugging in a crash situation rather difficult due to the -
> autorelease call.
Actually it is not that difficult to solve read on:
<http://developer.apple.com/technotes/tn2004/tn2124.html>
MallocStackLogging and MallocStackLoggingNoCompact in combination
with /usr/bin/leaks. Also useful is an editor that colors
copy,retain,release in red so you can see where you do one but not
the other (XCode sadly doesn't allow customizing for specific keywords).
>
> But what about number three, for instance? Is it better than
> number two? Does Apple really recommend number one? Why?
>
> Sorry to list moderators, administrators, and readers for a
> question on what may be largely a matter of taste, but as I say,
> I'm not as interested in opinions on which is *better* as I am in
> becoming more experienced with descriptions of the merits and
> possible problems encountered with each type of accessor.
>
> Many thanks,
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden