I'm in the process of learning Objective-C and I've read that I should be using both bracket notation exclusively and dot notation exclusively. Is there any practical benefit to using one over the other, or is it personal preference?
-
1possible duplicate of [Style: Dot notation vs. message notation in Objective-C 2.0](http://stackoverflow.com/questions/1249392/style-dot-notation-vs-message-notation-in-objective-c-2-0) – kubi Jul 12 '12 at 23:41
-
1[Here's a nice Big Nerd Ranch posting I found talking about dot notation syntax](http://weblog.bignerdranch.com/83-83/) which I think applies directly to your very subjective question. – Michael Dautermann Jul 12 '12 at 23:45
-
duplicate as pointed by @kubi. – Tatvamasi Jul 12 '12 at 23:55
-
Also, this is a pure style question. There is no right or wrong answer. The only thing that will affect this answer is your background. People from a Smalltalk background who have used Objective-C since its birth will most likely prefer bracket notation, while people from Java or C-anything will probably prefer dot notation. – borrrden Jul 13 '12 at 02:33
-
@MichaelDautermann That BNR article is 3 years old. Dot notation is here to stay. Borrrden is right, this is largely a stylistic issue and you should be sticking with the style that Apple recommends, *especially* if you are just starting out. – kubi Jul 13 '12 at 15:19
2 Answers
Dot notation is the current best way to access properties in Objective-C. The arguments for one paradigm or another are largely stylistic, and Apple's style is dot notation. As an Objective-C beginner, you should always strive to emulate Apple's style. Whoever told you to exclusively use bracket notation is incorrect.
And, though you didn't ask this I'm going to mention it here because it's a perpetual source of confusion for people just learning Obj-C. The following setters are identical
[self setFoo:42];
self.foo = 42;
the following getters are identical
bar = [self foo];
bar = self.foo;

- 48,104
- 19
- 94
- 118
You should always use dot notation whenever you're dealing with properties. Not only is it quicker to write (self.foo
has less characters than [short foo]
) but more importantly it makes chained messages easier to understand. For example:
self.myTextField.text.length
is easier to understand than
[[[self myTextField] text] length]
Additionally, it makes it less likely that you'll mess up the punctuation (Making sure that you have the correct number of brackets is oftentimes a pain).

- 10,940
- 23
- 85
- 140