3

Possible Duplicate:
Is there a difference between setting a property with the dot or the bracket syntax?

If I define a label in the .h file and I want to change the text of it in the .m file, is there really a difference between using label.text = @"..." and [label setText:@"..."]? They appear to do exactly the same thing, and if that's the case, then why the different ways to do it?

Community
  • 1
  • 1
cory ginsberg
  • 2,907
  • 6
  • 25
  • 37
  • possible duplicate of [Dot notation using setter?](http://stackoverflow.com/questions/2207810/dot-notation-using-setter), [Is dot syntax or bracket syntax correct?](http://stackoverflow.com/questions/10786714/is-dot-syntax-or-bracket-syntax-correct-for-property-access), [Message syntax vs. dot syntax: what's the difference?](http://stackoverflow.com/questions/4259946/objective-c-message-syntax-vs-dot-syntax-whats-the-difference) – jscs Jul 25 '12 at 04:33

1 Answers1

10

There is no difference in the inner workings. The first way requires the type of label to be such that the compiler could verify the presence of the property; the second way works even if the compiler does not know the exact type (i.e. when the type is an id).

The second syntax is the original one; the first syntax was added for convenience and readability. There are different schools of thought on the subject of using these syntaxes: some people will tell you not to use one syntax or the other, and provide very valid reasons for it. In the end, though, this is your choice: you should pick one syntax, and stay with. As long as you are consistent and your readers know of your preference, they should have no trouble reading and maintaining your code either way.

Teej
  • 12,764
  • 9
  • 72
  • 93
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523