2

Possible Duplicate:
Style: Dot notation vs. message notation in Objective-C 2.0
Objective-C Property Access

Before giving me thumbs down for noob-question: I have no idea what to search for when it comes to this question.. Anyway, let's say I'm trying to change the background color of an imageview, what is the difference between:

imageView.backgroundColor = [UIColor orangeColor];

and

[imageView setBackgroundColor:[UIColor orangeColor]];

I know that the first one can be used to GET the actual value, that I can say

UIColor *imageViewColor = imageView.backgroundColor;

and that I can't do this with the other one, but why does then the other one exist? Or is it simply wrong to use the first one as a way of setting values? But then, why does it work?

This question has nothing spesific to do about changing the background color of and imageView by the way, I'm just asking in general, what's the actual difference..

Community
  • 1
  • 1
Sti
  • 8,275
  • 9
  • 62
  • 124
  • 1
    Possible duplicate: http://stackoverflow.com/q/1398305/836214 – Krease Jan 11 '13 at 19:45
  • 1
    Also [What's the difference between using accessors and dot syntax?](http://stackoverflow.com/questions/1258360/whats-the-difference-between-using-obj-c-accessors-and-using-dot-syntax?lq=1), [Dote notation vs. message notation](http://stackoverflow.com/questions/1249392/style-dot-notation-vs-message-notation-in-objective-c-2-0) – jscs Jan 11 '13 at 19:58
  • The only notable thing to say is that you shouldn't use the dot syntax with things that aren't properties. Except for this, if they both work, why should one be better? – Ramy Al Zuhouri Jan 11 '13 at 20:02
  • @Chris Yes and I'm sorry about that. I assumed there would be a few of these, I just didn't know what to search for, especially since English isn't my primary language.. Dot notation, accessors, these are terms I haven't used much. Honestly, I think this question is easier to reach for someone who doesn't know. – Sti Jan 11 '13 at 20:02

3 Answers3

4

Absolutely nothing. They're equivalent.

(Well, the syntax is different.)

By the way,

and that I can't do this with the other one

is not true. Both notations have setter and getter syntaxes:

object.property = value;

is equivalent to

[object setProperty:value];

and

variable = object.property;

is equivalent to

variable = [object property];

Furthermore,

but why does then the other one exist?

Because in older versions of Objective-C ("Objective-C 1.0") there were no properties. One had to manually implement setter and getter methods, and this was the generally accepted naming convention.

Or is it simply wrong to use the first one as a way of setting values?

No, it isn't.

But then, why does it work?

Because it's not "wrong", that's why. (But then, why don't you google it?)

  • Oh, I didn't know that, thanks. But then why do both exist? And which is the preferred one? I use both randomly. There's never a time when one of them works and the other won't? – Sti Jan 11 '13 at 19:45
  • 1
    @Sti Updated the reason why both exist. Also, use them consistently. Pick one. I generally use the dot notation since it's more readable. –  Jan 11 '13 at 19:47
3

The dot notation is a short-form way of accessing @property values - see this related SO question: Objective-C Property Access

the compiler converts foo.property to [foo property] during compile-time

The question Dot notation vs. message notation for declared properties discusses merits of choosing one style over the other

Community
  • 1
  • 1
Krease
  • 15,805
  • 8
  • 54
  • 86
  • In few words choose one or another, but do not use the dot notation for things that aren't properties. – Ramy Al Zuhouri Jan 11 '13 at 19:58
  • @RamyAlZuhouri Do you have an example of that? – Sti Jan 11 '13 at 20:05
  • In the link at the bottom of the answer bbum makes a nice example. – Ramy Al Zuhouri Jan 11 '13 at 20:10
  • This is a point worth emphasizing: the compiler converts foo.prop into [foo prop]. This means that you must synthesize the member before you can access it as foo.prop. It also means that you are getting memory management "for free" via the setter/getter and if you omit the instance specifier (eg instead of self.foo=... you write foo=...) you will get a memory leak. – jeffmurphy Jan 11 '13 at 21:05
0

The short answer is the two are the exact same in terms of execution.

Now, to be more precise, Objective-C added properties in 2.0. The dot syntax is a shorthand way of calling accessor methods on a property. Accessor methods are methods you use to get or set the value of a property. Sometimes you'll hear them referred to as getters and setters.

So what happens when you say imageView.backgroundColor = someColor? Basically, the compiler looks for a setBackgroundColor method and invokes that method. Similarly, if you say UIColor *color = imageView.backgroundColor, the compiler looks for a method called backgroundColor that returns a UIColor and invokes that method.

The thing to realize here is that dot syntax really should only be used when invoking accessor methods on properties. Objective-C does allow you to use dot syntax for general message sending, but that tends to obscure what is going on in a project and makes code maintainability a big issue.

jmstone617
  • 5,707
  • 2
  • 24
  • 26