I'm new to objective C and I just wanted to get a general clarification on when to use a pointer and when not to, when declaring instance variables.
The example I can think of is UIView vs. a BOOL. UIView I would make a pointer, BOOL I would not (because the compiler yelled at me).
Any general guidance would be awesome.
Cheers,
If it's an object, you use pointer notation. All of the c types (int, BOOL, long, etc) are not objects and thus you only use a pointer if you want a pointer to their memory location:
NSObject *obj;
UIView<Protocol> *obj;
int integerVar;
BOOL isTrue;
A special case is id
, which is itself a pointer to an object, so you don't need the *
:
id obj;
Slightly tricky:
NSInteger int;
NSNumber *number;
NSInteger is the appropriate platform-specific int type, whereas NSNumber is an object that can contain an int, float, or what have you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With