Reputation: 11452
I am fairly new with Objective C and getting my feet wet! I have came across with two different notations in syntax,
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
Upvotes: 4
Views: 235
Reputation: 7644
I don't think there is any standard but I prefer the "Square bracket notation" for calling methods and the "dot notation" for accessing properties. Though the dot notation works the same, it helps differentiate between the two operations
Upvotes: 1
Reputation: 63667
Use dot notation to refer to properties and square notation to call methods. Both result in a message send, but at least you are separating state vs behaviour.
Upvotes: 3
Reputation: 92335
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).
Upvotes: 4
Reputation: 73608
Well if I am not wrong, the square notation
is more native. This is what was more prevalent.
The dot notation
has come into being recently.
I have seen code with both styles being used...
Upvotes: 1