• 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: [newbie] Use of accessors versus direct access of ivars
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [newbie] Use of accessors versus direct access of ivars


  • Subject: Re: [newbie] Use of accessors versus direct access of ivars
  • From: "Adam Foltzer" <email@hidden>
  • Date: Fri, 9 Jan 2009 00:53:08 -0500

Hi Stuart,

I'm still something of a neophyte, but a theme I've found in threads here is
that consistent use of accessors tends to save memory management headaches
if you're not using garbage collection. If you use @property and @synthesize
to generate your accessors, and then use them to both initialize ( [self
setIvar:[[MyObj alloc] init] ) and release ( [self setIvar:nil] ) your
ivars, you bypass most common memory management problems. If you want to add
custom behavior, be sure to write them according to these guidelines to keep
this benefit:

http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAccessorMethods.html

Essentially, accessors let you put the majority of your memory management
code in one place, rather than forcing you to pay attention to it in every
method that uses your ivars.

The more abstract benefit of using accessors is that your code becomes
easier to maintain and update. If you decide you want to store an ivar
differently, you can just change its accessors, rather than changing every
method that directly accesses it.

Of course, accessors are not a magic bullet for either of these issues, they
just help in many ways you might not anticipate.

Cheers, and HTH,
Adam

On Fri, Jan 9, 2009 at 12:27 AM, Stuart Malin <email@hidden> wrote:

> I have a class with ivars for which there are accessors. I understand that
> the importance of using a setter method (or to use setValue:ForKey:) to set
> the ivar so that KVO operates. However, Is there a compelling reason to use,
> in the methods of a class, a getter method (or valueForKey:) versus directly
> accessing an ivar of the class?
>
> I have re-read both the Key-Value Observing Programming Guide and Key-Value
> Coding Programming Guide, and have not derived guidance from them on this
> question. Perhaps I have missed something.
> _______________________________________________
>
> 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
>
_______________________________________________

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

References: 
 >[newbie] Use of accessors versus direct access of ivars (From: Stuart Malin <email@hidden>)

  • Prev by Date: Re: Getting to the Console Logs
  • Next by Date: Re: NSPersistentStoreCoordinator Wars Episode I: The Phantom Lock Menace
  • Previous by thread: [newbie] Use of accessors versus direct access of ivars
  • Next by thread: Re: [newbie] Use of accessors versus direct access of ivars
  • Index(es):
    • Date
    • Thread