• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Instance variables: access directly vs. getter / setter
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Instance variables: access directly vs. getter / setter
      • From: Matthias Arndt <email@hidden>
References: 
 >Instance variables: access directly vs. getter / setter (From: Matthias Arndt <email@hidden>)
 >Re: Instance variables: access directly vs. getter / setter (From: Graham Cox <email@hidden>)
 >Re: Instance variables: access directly vs. getter / setter (From: Matthias Arndt <email@hidden>)

  • Prev by Date: Re: Instance variables: access directly vs. getter / setter
  • Next by Date: Re: Instance variables: access directly vs. getter / setter
  • Previous by thread: Re: Instance variables: access directly vs. getter / setter
  • Next by thread: Re: Instance variables: access directly vs. getter / setter
  • Index(es):
    • Date
    • Thread