Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is preferred in Objective-c dot notation or square bracket notation?

I'm reading a book - Big Nerd Ranch iOS Programming. It says that dot notation is not recommended as it obfuscates code. I am simultaneously watching a Stanford Course for iOS programming and in it he is using dot notation extensively there. What would you recommend? I personally lean to bracket notation more.

Could you please explain how to covert this code to bracket notation?

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

As I understand it should be:

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

Is it correct?

like image 948
Dvole Avatar asked Jul 13 '12 16:07

Dvole


People also ask

What situations use dot notation and bracket notation?

The dot notation is used mostly as it is easier to read and comprehend and also less verbose. The main difference between dot notation and bracket notation is that the bracket notation allows us to access object properties using variable.

What are square brackets Objective-C?

In Objective-C, the square brackets ([]) were added for message sending. While the square brackets had previously only been used for indexing an array, the intent of the brackets can be easily determined by the context. `[object message]; // Definitely a message send!

Why do we use bracket notation?

Bracket Notation & Variables Bracket notation gives us the ability to use variables to access values in an object. This is especially helpful with the value of the variable changes.

How do you use dot notation?

Dot notation is one way to access a property of an object. To use dot notation, write the name of the object, followed by a dot (.), followed by the name of the property. Example: var cat = { name: 'Moo', age: 5, }; console.


1 Answers

This is a matter of personal choice. There are those that argue that dot notation makes it unclear that messages are being sent (methods are being called) since it looks just like C-style structure element access. The other side of argument is that dot notation is easier to type, easier to read, and more concise.

As someone who has been writing Objective-C since before dot notation was introduced (in Objective-C 2.0), I can understand the arguments on both sides, but prefer to use dot notation myself. That said, I do think it's important for people beginning with Objective-C to understand that dot notation syntax is converted to standard accessor method calls when compiled. I think the authors of the Big Nerd Ranch book probably have a similar attitude, and that's a big part of why they decided to use bracket notation in the book.

So in short, do what you like best. Both are valid, and the choice between the two is essentially a matter of style. No matter which you choose, make sure you understand that both styles produce equivalent compiled code.

EDIT: I forgot to answer the question about converting dot notation to bracket syntax. You're close, but what you've written is wrong and won't actually compile. It should be: [[self display] setText:[[[self display] text] stringByAppendingString:digit]] . If I were writing it I'd split it into two lines (well, really I'd use dot notation):

NSString *stringWithDigit = [[[self display] text] stringByAppendingString:digit]; [[self display] setText:stringWithDigit]; 

EDIT 2: It has been more than 3 years since I wrote this answer. I just wanted to note that these days, a great many more Apple framework classes have had things that were previously regular methods converted to @properties (e.g. -[NSArray count]) presumably for better Swift interop. This has led me to use dot notation even more liberally than I used to.

like image 152
Andrew Madsen Avatar answered Oct 18 '22 00:10

Andrew Madsen