• 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: Properties vs Instance Variables
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Properties vs Instance Variables


  • Subject: Re: Properties vs Instance Variables
  • From: Steve Christensen <email@hidden>
  • Date: Tue, 26 Apr 2011 21:07:07 -0700

On Apr 26, 2011, at 8:28 PM, Guy Steven wrote:

> I am currently learning objective c and cocoa
>
> I believe I understand the difference between properties and instance
> variables, and the effect of using properties as opposed to instance
> variables viz a viz KVO and memory management.

A property is really get and/or set methods, so...

> What I can't understand is why you would access instance variables directly
> within a class. My quess is speed. Is there any other reason?

...a property may or may not simply do a "return foo;" or "foo = value;" as its behavior. It may, for example, lazily create an object when called or it may have some other side effects that are required when accessed by a caller; those same requirements might not be needed or desired when using the ivar from within the class.

> And related to this, why give instance variables a name different to the
> name of the property.

You might want to name an instance variable _foo or mFoo, etc., to distinguish it from a local variable or parameter. (And that would be personal style.)

> Again, I suspect it may be a matter of style, but would like to know if
> there are cases where it is a necessity.

I believe it's only necessary if you want to do a straight "@synthesize foo;", which would expect the instance variable to have the same name as the property.

_______________________________________________

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: Properties vs Instance Variables
      • From: Kyle Sluder <email@hidden>
References: 
 >Properties vs Instance Variables (From: Guy Steven <email@hidden>)

  • Prev by Date: Re: Properties vs Instance Variables
  • Next by Date: Re: Properties vs Instance Variables
  • Previous by thread: Re: Properties vs Instance Variables
  • Next by thread: Re: Properties vs Instance Variables
  • Index(es):
    • Date
    • Thread