In Objective-C, I've seen, for example:
UIPickerView *tweetPicker
and
UIPickerView* tweetPicker
What does the asterisk mean (I know, the first part is a dupe...) and why does it go on different parts of the deceleration in different contexts?
In Objective-C, I've seen, for example:
UIPickerView *tweetPicker
and
UIPickerView* tweetPicker
What does the asterisk mean (I know, the first part is a dupe...) and why does it go on different parts of the deceleration in different contexts?
The asterisk indicates that the variable is a pointer. In objective-C all objects are represented through pointers.
As far as where it is put, this is a style thing. My personal style is actually with spaces around the asterisk.
UIPickerView * tweetPicker
In all three cases they mean the same thing, but with different styles.
Edit: Spacing does not matter, but position does in corner cases dealing with keywords such as if const
refers to a constant pointer or a pointer to a constant value. But your question was related to spacing, which does not matter.
In the exact case you're showing, there is no difference. Some people like to think of tweetPicker
being of type UIPickerView *
, that is, a pointer to a UIPickerView
. These people usually write it as
UIPickerView* tweetPicker;
Other people prefer to think of it like *tweetPicker
is a UIPickerView
, that is, dereferencing the pointer gives a UIPickerView
. Those people usually write:
UIPickerView *tweetPicker;
I prefer the latter, because the C (and Objective-C because of that) syntax supports it better. Take, for example, the following variable declarations:
int* a, b, c;
int *a, *b, *c;
At first glance, the novice C (or Objective-C) programmer might say "those are the same", but they're not. In the first case, b
and c
are regular integers, in the second case, they're pointers. a
is a pointer in both cases.
From my perspective, the concept of a "type" is so weak in C anyway (what with the behaviour of typecasting and the like) that extending that concept one step further to pointer variables is crazy - especially with the automatic & silent conversions to and from void *
or id
that you get.