Thursday, April 21, 2016

How to format the star in Objective-C?

For those still working in Objective-C, how do you format the code when you are using pointers?

Do you use:

UIView *view;

Or

UIView* view;

Or

UIView*view;

While there is not right or wrong as all of them are correct from compiler point of view, the preferable syntax becomes clear when we have multiple values, view1 and view2:

Each of the above will translate into

UIView *view1, *view2;

UIView* view1,* view2;

UIView*view1,*view2;

Obvious the winner is the first version.

The last two version have the potential issue of forgetting the *, making the code incorrect:

UIView* view1, view2;

UIView*view1, view2;


To conclude the proof, the winner is:

UIView *view;


How about the strong-typed array?

The suggested syntax is the following:

NSArray <NSString *> *array;

The proof is step by step.


We concluded that between the type and the variable name should be a space. 

So we have:

<NSString *>

Also * should be near the variable name, so:

*array

Adding NSArray at the beginning and adding a space for nicer syntax, we have:


NSArray <NSString *> *array;




1 comment:

Note: Only a member of this blog may post a comment.