Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dot notation vs square brackets and casting in Objective-C

Which of the following is best practice in Objective-C?

UITableView* view = (UITableView*) [self view];
[view setSeparatorColor:[UIColor blackColor]];
[view release];

vs.

((UITableView*) self.view).separatorColor = [UIColor blackColor];

Or is there a better way of writing this? self.view is a UIView*.

I'm asking both because I have a weird looking cast (maybe there's a better way?) and because of the following text from the official documentation, which hints that it's more than just a matter of style or personal preference:

A further advantage is that the compiler can signal an error when it detects an attempt to write to a read-only declared property. If you instead use square bracket syntax for accessing variables, the compiler—at best—generates only an undeclared method warning that you invoked a nonexistent setter method, and the code fails at runtime.

like image 779
rid Avatar asked Jun 09 '11 12:06

rid


People also ask

What do square brackets mean in Objective-C?

The code on the second line is not reading the instance variable directly. It's actually calling a method named caption. In most cases, you don't add the "get" prefix to getters in Objective-C. Whenever you see code inside square brackets, you are sending a message to an object or a class.

What is the difference between bracket and dot notation?

Dot notation is faster to write and easier to read than bracket notation. However, you can use variables with bracket notation, but not with dot notation. This is especially useful for situations when you want to access a property but don't know the name of the property ahead of time.

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.

What do square brackets mean in syntax?

[ ] Anything shown enclosed within square brackets is optional unless indicated otherwise. The square brackets themselves are not typed unless they are shown in bold. | A vertical bar that separates two or more elements indicates that any one of the elements can be typed.


2 Answers

Well.... dot notation compiles down to square brackets in the end, but it is down to personal preference. I personally avoid dot notation unless I am setting / accessing a scalar type, it is too easy to look at the following for instance...

view.step = 2.0;

... and not know where step is a scalar property, or has a setter method etc. I prefer to be explicit and would use...

[view setStep:2.0];

But again personal preference I guess.

like image 176
Simon Lee Avatar answered Sep 22 '22 19:09

Simon Lee


2 things

  1. You didn't ask that but - I used to love those "One lines" in the beginning, but after some time when you get back to the code it is less readable.

  2. the dot seems more readable to me

I would prefer that -

    UITableView* view = (UITableView*)self.view;
    view.setSeparatorColor=[UIColor blackColor];

But in the end it is a matter of your own preferences.

like image 33
shannoga Avatar answered Sep 22 '22 19:09

shannoga