Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C instance variable pointers

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,

like image 767
Meroon Avatar asked May 27 '09 23:05

Meroon


1 Answers

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.

like image 87
Andrew Pouliot Avatar answered Feb 07 '23 02:02

Andrew Pouliot