Possible Duplicate:
What's the difference between dot syntax and square bracket syntax?
I’ve tried using these two expressions:
-
[[[self display] text] stringByAppendingFormat:digit]; -
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.
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:
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:
Also note that you should have a format string as the first argument to
stringByAppendingFormat:. If yourdigitstring accidentally had any format specifiers in it, it would cause a crash. A better choice here would bestringByAppendingString:—[oldText stringByAppendingString:digit].