Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

swiftd

macrumors newbie
Original poster
Jul 12, 2009
11
0
Hey guys,

I'm currently learning Objective C and have a question about asterisk placement (I generally understand pointers using asterisks to declare them). In the book I'm reading (iPhone Development for Dummies) the asterisk is placed differently a few times, and I'm not sure why. Could someone tell me why it's in different locations in the following examples?

NSString* blah
(NSString *) blah
IBOutlet UITextfield *textField

And also why there is no asterisk in the following statement?

CGRect rect = self.view.frame;

Thanks for reading!
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
In not the order you asked:
CGRect is a structure, and that's a local instance. You can have pointers to structures on the heap or local instances, though in Foundation most struct *s are typedef'd to TypeRef (i.e. CGRectRef) so you don't have *s about when dealing with structures, to delineate from Objects.
http://developer.apple.com/DOCUMENT...l#//apple_ref/doc/uid/TP30000955-CH2g-C016213

NSString* blah
IBOutlet UITextfield *textField

These two just show a difference in style. I prefer that the * go with the varaible like the second example, for this reason:
Code:
int *x,y,z,a;
in this case, if you put the * next to int, it might mislead one into thinking everything is an int *, instead of x being an int * and y,z, and a being ints.

(NSString *) blah
In this case, i would expect to see this in the signature of a class or object method. In this case, the types are always parenthesized. If you saw this in another context, that would be odd.

-Lee
 

Cromulent

macrumors 604
Oct 2, 2006
6,802
1,096
The Land of Hope and Glory
An important thing to remember when programming in Objective-C (and most other languages) is that whitespace is meaningless. All you need to do is make sure that keywords are kept together. int and in t are different for instance.

Code:
int* x;
int * x;
int *x;
int*x;

are all exactly the same. I could write it as:

Code:
int     *        x         ;

if I wanted and it would still be fine.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.