Re: Properties vs Instance Variables
Re: Properties vs Instance Variables
- Subject: Re: Properties vs Instance Variables
- From: Quincey Morris <email@hidden>
- Date: Tue, 26 Apr 2011 21:26:24 -0700
On Apr 26, 2011, at 20:28, Guy Steven wrote:
> 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.
>
> 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?
>
> And related to this, why give instance variables a name different to the
> name of the property.
Well, TBH, if you're asking the question you don't yet fully understand the difference.
A property is a set of public behaviors of a class, one through which the property value flows. (Let's ignore the fact that classes can use properties privately. The principle is the same.)
An instance variable is a piece of backing store that a property may or may not want to use.
Here's an analogy that may help. Let's say you go to a restaurant and order a bowl of stew. Eventually, a bowl of stew appears and is placed in front of you.
Back in the kitchen, most likely, there is a big pot of stew.
What's on your plate is stew, and what's in the kitchen is stew, yet if you're the cook you're not likely to confuse them. Indeed, the bowl of stew that arrives at your table may have some melted cheese on top, which probably wasn't in the stew pot, but got sprinkled there when your order was being filled.
More radically, there may be *no* pot of stew in the kitchen. It's not impossible that the kitchen staff didn't just run to the convenience store next door and buy some frozen stew which it then heated and served to you.
Assuming that you, as cook, understand the difference between a pot of stew simmering on the stove and an order being prepared for a customer, then you, as developer, should understand the difference between an instance variable and a property.
In many cases there's no particular reason to give an instance variable a different name from its property (stew is stew, after all). In some cases it's part of a naming convention designed to avoid clashes between instance variable names and local variables, or between your instance variable names and existing instance variable names in superclasses, which you often aren't aware of explicitly unless something goes wrong.
_______________________________________________
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