2

There are three different syntax styles for accessing properties of an object:

myProp = value;
self.myProp = value;
[self setMyProp: value];

Are these purely style choices or is there difference in substance?

  • study these answers until you know them at inside and out - - - many memory management issues that i see beginners having with Objective-C is the root of not understanding this and what the @property/@synthesize directives do. – bshirley Jun 30 '11 at 14:19
  • 1
    The first one isn't a property access. – JeremyP Jun 30 '11 at 14:23
  • Related questions http://stackoverflow.com/questions/5210188/property-question http://stackoverflow.com/questions/2736762/initializing-a-readonly-property http://stackoverflow.com/questions/5466496/why-rename-synthesized-properties-in-ios-with-leading-underscores/5466601#5466601 http://stackoverflow.com/questions/4511700/synthesizing-a-property-without-instance-variables – Robert E. Menteer II Jun 30 '11 at 19:56

4 Answers4

4
self.myProp = value;

and

[self setMyProp: value];

are style choices, as they are using accessors to set values. That is, self.myProp essentially is the same as calling [self setMyProp] or [self myProp]. It will implement whatever mechanism you define in the @property tag (retaining, releasing as needed, etc).

However,

myProp = value;

is substantially different as it is simply an assignment. No consideration for releasing myProp's original pointer, retaining the new value, etc.

MarkPowell
  • 16,482
  • 7
  • 61
  • 77
  • So as long as we're not doing anything funky with the getter/setter names all of these access the same variable. The only difference is the direct assignment ignores any copy/retain/week/strong attributes defined for the property. The implication of this is direct assignment should rarely be used outside custom getters/setters and exclusively within them. – Robert E. Menteer II Jun 30 '11 at 15:15
  • Unless your property is `retain` or `copy`; direct access bypasses that behavior. This is also why you see many folks use `@synthesize myProp = myProp_;` to create an ivar with a different name. This question has come up many times; search the archives. – bbum Jun 30 '11 at 16:50
2

The first is an direct assignment to the iVar myProp. The second and third are the same. You should prefer the second or third, because when you use @synthesize the setter does the memory management for you.

Pierre
  • 2,019
  • 14
  • 12
1
myProp = value;

This is a direct assignment to a C variable. It's not a property access at all.

self.myProp = value;

This is syntactic sugar for:

[self setMyProp: value];

What does that mean? It means that the former is translated by the compiler into the latter before emitting the code for it. That is the only meaning of dot notation. As long as the class declares method myProp and setMyProp: dot notation will work. myProp does not need to be a property in the formal sense (i.e. declared with @property). It also does not need to be synthesized. There does not need to be a single instance variable to back it. As long as the two methods exist at run time (or the getter if you never assign to the property), you can use dot notation. It's orthogonal to Objective-C properties.

So the answer is that, yes, for your second two examples, it is purely a matter of style. Personally, I hate dot notation. It's an unnecessary pollution of the syntax of the language (by which I mean we now have two ways of expressing the sending of messages, one of which looks identical to a completely different language feature). /rant

JeremyP
  • 84,577
  • 15
  • 123
  • 161
1

Your examples #2 and #3 compile identically, but #1 has an important difference -- it does not call the setter. This makes it useful (required) inside the setter or else you will create an infinite loop.

For example, this setter creates an infinite loop:

- (void)setMyProp:(int)value
{
self.myProp = value;
}

The same is possible in the getter.

Michael Mangold
  • 1,741
  • 3
  • 18
  • 32