[Solved] Re: clarification regarding properties

On Mar 27, 2009, at 09:24, WT wrote:

If I understand correctly, there *is* a difference between using self.foo and just foo, where foo is a property, right?

foo = whatever

just sets the value of foo to the value of whatever, but

self.foo = whatever

actually calls the method setFoo: with whatever as its argument. This difference is important because the setter method might do some necessary memory management which the simple unqualified assignment never does.

Is my understanding correct?

You got a "correct" response, and I'm going to add an "incorrect" one ...

The 'foo' in 'self.foo' is a property. The 'foo' in 'just foo' is *not* a property, but an instance variable. It's really important to know that the two are entirely different things, even when they are named the same. Whether the property foo even uses an instance variable foo is an implementation detail of your class. A property *may* be implemented using an instance variable for its storage requirements, and the instance variable *may* have the same name as the property, but neither of those things are requirements.

Incidentally, the syntactic equivalent of Java's 'this.foo' is probably Objective-C's 'self->foo', which *is* is the same thing as just plain 'foo'. Any resemblance between 'this.foo' and 'self.foo' is an optical illusion. ;)

Incidentally (again), I believe the current recommendation is generally *not* to use property accessors (setters and getters) inside initializers (init... methods) and dealloc methods. But that seems to be a somewhat hot topic, so you might see differing opinions on it.


Cocoa-dev mailing list (Cocoa-dev@xxxxxxxxxxxxxxx)

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 S...


This email sent to maillists@xxxxxxxxx