Re: Beginner Question Re: Memory Management
Re: Beginner Question Re: Memory Management
- Subject: Re: Beginner Question Re: Memory Management
- From: Eric Hermanson <email@hidden>
- Date: Tue, 23 Jun 2009 01:37:50 -0400
Objective-C 2.0 supports not only property getter/setter synthesis,
but instance variable synthesis. This means that you do not even have
to declare the instance variable. As long as the property is defined
correctly, the instance variable (and the getter/setter) will be
created for you. I prefer not defining the instance variable at all,
that way there's no confusion about the access; you always use the
synthesized getter/setter.
UNFORTUNATELY, the iPhone Simulator does not support instance variable
synthesis (at least it didn't in 2.x, maybe that changed in 3.0).
This means that you cannot have code that works on both iPhone and
Simulator if you want to take advantage of the instance variable
synthesis.
- Eric
On Jun 23, 2009, at 1: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.
_______________________________________________
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