Objective-C syntax paradigm

I am new to Objective-C and wet my feet! I came across two different notations in the syntax,

  • Dot notation
  • Square bracket designation

I would like to ask: what would be preferable? Since I learn this right after java, I am much more familiar with dot notation. Therefore, it should be considered that this is a normal template for any code that I write? Is industry standard notation used?

thanks for the help

+4
source share
4 answers

Point notation is only available for properties and, of course, C. structures. Do you prefer:

foo.aProperty = bar.anotherProperty; 

or

 [foo setAProperty:[bar anotherProperty]]; 

... it's a matter of taste. I personally prefer the second, because it is absolutely clear that two calling methods are involved. You cannot say at a glance in this case:

 CGFloat x = myView.frame.origin.x; 

This is equivalent to:

 CGFloat x = [myView frame].origin.x; 

The second example clearly shows that the method call is involved, but the first example tends to be more readable.

So, use what suits you, both are fine (although I believe most developers tend to use the former, in part because they are faster to type and tend to be more picky).

+4
source

Use dot notation to refer to properties and square notation to invocation methods. Both results in a message being sent, but at least you separate state behavior and behavior.

+3
source

Well, if I'm not mistaken, square notation is more native. This is what was more common. dot notation arose recently.

I saw code using both styles ...

+1
source

I don’t think there is any standard, but I prefer “square notation” for calling methods and “dot notation” for accessing properties. Although dot notation works the same, it helps to distinguish between two operations

+1
source

All Articles