Re: Instance variables: access directly vs. getter / setter
Re: Instance variables: access directly vs. getter / setter
- Subject: Re: Instance variables: access directly vs. getter / setter
- From: Graham Cox <email@hidden>
- Date: Fri, 9 Oct 2009 22:32:31 +1100
On 09/10/2009, at 10:14 PM, Matthias Arndt wrote:
[self setIvar:[self ivar] + 1];
instead of
iVar++;
looks a bit confusing to me.
Maybe confusing, but the two are not necessarily equivalent. In the
second case, you are merely incrementing the ivar. No-one else would
know anything about it - you'd have to take further steps to notify
others of the change, including any potential subclasses of your
class. Sometimes you might want that, but I would suggest that 99% of
the time, you do not.
In the first case, interested observers using KVO to track changes to
<ivar> will get automatically notified, and any subclasses that have
overridden -setIvar: or -ivar also get called as they should.
--Graham
_______________________________________________
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