Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective C syntax paradigm

I am fairly new with Objective C and getting my feet wet! I have came across with two different notations in syntax,

  1. Dot notation
  2. Square bracket notation

I would like to ask, Which would be more preferable? As I am learning this straight after java I am much more familiar with dot notation. So should consider it to be a normal pattern for whatever code I write? Does industry standardised which notation to use?

Thanks for your help

like image 742
TeaCupApp Avatar asked Jan 20 '23 00:01

TeaCupApp


1 Answers

The dot notation is only available for properties and of course C structs. Whether you prefer:

foo.aProperty = bar.anotherProperty;

or:

[foo setAProperty:[bar anotherProperty]];

...is a matter of taste. I personally prefer the second one because it's absolutely clear there's two method calls involved. You can't tell at a first glance in this case:

CGFloat x = myView.frame.origin.x;

This is equivalent to:

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

In the second example, it's clearly visible that a method call is involved, but the first example tends to be more readable.

So use whichever suits you, both are okay (though I guess most developers tend to use the first one, partly due to the fact it's faster to type and tends to be more legible).

like image 80
DarkDust Avatar answered Jan 31 '23 18:01

DarkDust