views:

353

answers:

2

When do you and when dont you need the * symbol (which is because in objective-c all variables like NSString are pointer variables)?

For example when do you need to do "NSString *" instead of just "NSString"?

+4  A: 

In Objective-C, all object references are pointers, so you always need the pointer operator when you declare with an Objective-C object.

For other types, the use is exactly the same as in C. Use pointers when you want to pass data structures or primitive types by reference.

Jason Coco
The exception is the "id" type, which is roughly equivalent to void* in C, and hence the star is included in the type.
Quinn Taylor
+2  A: 

You use the asterisk for all Objective-C objects (such as NSDictionary, NSString, NSNumber).

For anything that is a primitive type (int, double, float) you don't need the asterisk. However, the NS prefix doesn't always mean that you must use an asterisk. Cocoa defines some structures (such as NSInteger, NSRect, NSPoint) that are are based on primitive types. You don't use the asterisk here either. An NSRect, for example, is just a structure of an NSPoint and NSSize, both of which are made up of 2 CGFloats (a primitive type).

You can pass a pointer to one of these primitive types or structures using the * notation.

Jeff Hellman