Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 12, 2009, 09:54 PM   #1
swiftd
macrumors newbie
 
Join Date: Jul 2009
[NOOB] Question about asterisk placement in Obj C

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!
swiftd is offline   0 Reply With Quote
Old Jul 12, 2009, 10:04 PM   #2
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: 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/DOCUMENTA...5-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

Last edited by lee1210; Jul 12, 2009 at 10:12 PM.
lee1210 is offline   0 Reply With Quote
Old Jul 13, 2009, 03:27 AM   #3
allegrocm
macrumors member
 
Join Date: Mar 2005
Location: Ames, IA
Send a message via AIM to allegrocm
(NSString*) blah

could also be casting blah from another pointer type, such as NSObject, to an NSString pointer.

ken
allegrocm is offline   0 Reply With Quote
Old Jul 13, 2009, 08:59 AM   #4
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Quote:
Originally Posted by allegrocm View Post
(NSString*) blah

could also be casting blah from another pointer type, such as NSObject, to an NSString pointer.

ken
Good call, out of context it was hard to place.

-Lee
lee1210 is offline   0 Reply With Quote
Old Jul 13, 2009, 09:30 AM   #5
Cromulent
macrumors 603
 
Cromulent's Avatar
 
Join Date: Oct 2006
Location: 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.
__________________
Neural Advance - Mac OS X, UNIX and Windows Development
Last.fm Profile | Extreme Metal Reviews
MP 4x 2.66Ghz Xeons / 6GB RAM / 640GB + 500GB + 750GB + 1TB HDDs / ATI Radeon 4870 / iPad 3
Cromulent is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Isntalling Asterisk on Mac OS X Snow Leopard suthan83 Windows, Linux & Others on the Mac 0 Mar 26, 2013 07:08 PM
PowerMac G4 Quicksilver Hard Drive Placement Question ewoh24 PowerPC Macs 14 Dec 16, 2012 07:32 PM
Question about placement of the Magsafe charger SteelBlueTJ MacBook Air 7 Sep 1, 2012 08:19 PM
Noob question nrfujnbfvj OS X 10.8 Mountain Lion 1 Jun 27, 2012 05:14 AM
Very basic question about clip placement in FCP or iMovie swingerofbirch Digital Video 6 Jun 9, 2012 06:44 AM

Forum Jump

All times are GMT -5. The time now is 06:27 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC