Re: Beginner Question Re: Memory Management
Re: Beginner Question Re: Memory Management
- Subject: Re: Beginner Question Re: Memory Management
- From: WT <email@hidden>
- Date: Tue, 23 Jun 2009 07:47:32 +0200
On Jun 23, 2009, at 7:29 AM, Quincey Morris wrote:
On Jun 22, 2009, at 21:31, WT wrote:
So, to all beginners out there, whenever you define a property,
remember always to refer to it, in its own class, by self.property
- at least when setting its value. It may be ok (depending on what
you're doing) to refer to it simply by property when *getting* its
value, but if the property is backed by an instance variable that
is an object (rather than a scalar type), it's *essential* that you
refer to it by prepending self when *setting* its value, or you'll
not be doing the memory management that you think you're doing.
I understand what you're trying to say here, but I'd urge you
*never* to describe it in these terms.
You say, explicitly, that it's a question of use the the correct way
(out of a choice of 2 ways) to refer to a property. But that's not
so. There's *one* way to refer to a property, and *one* way to refer
to an instance variable. The fact that they may have similar names
is purely an accident. (Well, not an accident, generally, but a
convenience.)
I'd say it's really, really, really important for beginners to
realize that properties and the instance variables that back them
are different, syntactically and semantically. Outside a class
implementation, the property is the only thing that's available (or
should be, since making instance variables public is usually a Bad
Idea), so clients of the class don't have to think about it. Inside
a class implementation, however, you need to decide *every time you
refer to a value* whether you mean the property value or the
instance variable value. You need to decide that even when you
*know* that the two values are currently the same, because in a
future piece of the implementation you may break the congruence, but
you don't want to break the existing code if you can help it.
It's not even about memory management. There's often memory
management involved when using a property's accessors, but there may
also be memory management involved when dealing with an instance
variable in other contexts.
FWIW.
Hi Quincey,
You're absolutely correct and I should have been more careful in the
way I said what I intended to say. I am myself still getting used to
the idea that properties don't have to be backed by instance
variables, which clearly shows that they're distinct concepts. I find
it oddly difficult to remember that, even though I understand the
distinction.
Thanks for the correction.
Wagner
_______________________________________________
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