0

Possible Duplicate:
What's the difference between dot syntax and square bracket syntax?

I've tried using these two expressions:

  1. [[[self display] text] stringByAppendingFormat:digit];

  2. self.display.text = [self.display.text stringByAppendingFormat:digit]

Where display is a UILabel and digit is an NSString.

I intend to set digit as the label's text, but when I was trying to run the program, only #2 gave the correct results.

So what exactly is the difference between those two expressions? Is it incorrect to use square brackets for the getter and setter?

I have checked this similar question, What's the difference between dot syntax and square bracket syntax?, but still cannot figure it out.

Community
  • 1
  • 1
dumbfingers
  • 7,001
  • 5
  • 54
  • 80
  • While this question bears a large similarity to the proposed duplicate, the real problem here is the lack of reassignment in the first snippet, which is not addressed in the other question. – jscs Dec 07 '11 at 20:21

2 Answers2

4

The two syntaxes are exactly equivalent. The dot syntax is transformed into the bracket syntax by the compiler. The problem you have is that you're not setting the value in your first snippet. If you change it to:

[[self display] setText:[[[self display] text] stringByAppendingFormat:digit]];

You will see the same result as with the dots.

I would suggest using a temp variable to make things a tad more readable, though:

NSString * oldText = [[self display] text];
[[self display] setText:[oldText stringByAppendingFormat:@"%@", digit]];

Also note that you should have a format string as the first argument to stringByAppendingFormat:. If your digit string accidentally had any format specifiers in it, it would cause a crash. A better choice here would be stringByAppendingString: -- [oldText stringByAppendingString:digit].

jscs
  • 63,694
  • 13
  • 151
  • 195
  • Thanks! It helped me a lot! But I've got another question: is there any regulations or common practice about when to use these two arguments? Thanks a lot! – dumbfingers Dec 07 '11 at 06:11
  • @ss1271: Which two arguments? – jscs Dec 07 '11 at 06:12
  • The square bracket one and the dot syntax one – dumbfingers Dec 07 '11 at 06:16
  • @ss1271: This question: http://stackoverflow.com/questions/1249392/style-dot-notation-vs-message-notation-in-objective-c-2-0 and the links you'll find there are about as complete a discussion as you'll find, I think. – jscs Dec 07 '11 at 06:21
  • Thank you! So, although these two arguments are programmatically correct, the argument #2 is a better answer. Because it uses dot syntax for setting attributes/properties and uses square brackets for sending messages or calling methods. Am I right about this? – dumbfingers Dec 07 '11 at 14:53
  • You're right in that you shouldn't use dot syntax for anything besides property access. Whether to use it at all, or which is better, is a matter of opinion. Using it still involves sending a message, however; the compiler turns both into exactly the same thing. Your terminology is a little confused: an "argument" is the thing that you pass to a method or function when you call it: `int x = 10; myfunction(x);` here `x` is the argument. These snippets are "statements". – jscs Dec 07 '11 at 20:10
2

Basically there is no difference between them.

I don't know if you mystically dropped a few code, but when you use -

  [[[self display] text] stringByAppendingFormat:digit];

You haven't assigned the expression result to your variable. you should:

   self.display.text = [[[self display] text] stringByAppendingFormat:digit];

Hope it will help

Shani

shannoga
  • 19,649
  • 20
  • 104
  • 169