Meroon
Meroon

Reputation: 3488

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,

Upvotes: 9

Views: 9182

Answers (3)

AriX
AriX

Reputation: 1697

Every Objective-C class, like NSString, NSObject, NSView, etc. needs to be a pointer, except for a few special types like NSUInteger, which is just a typedef for int I believe.

NSString *stringyString = @"THIS STRING IS STRINGY!!!11";
NSOpenPanel *openPanel;
NSObject *objectyObject;
NSUInteger integeryInteger = 7;

The only thing that won't is id, because it is a pointer to any object.

id pointerThatCanBeSetToAnyObject = [NSString stringWithString:@"HEYYYY"];

Only C variable types like int, float, BOOL, etc. will not require a pointer, except for C strings like char arrays.

int SEVEN = 7;
float SIXPOINTTWO = 6.2;
char *characterArray = "HEYYYYY";

Finally, CoreFoundation classes have a kind of a hybrid; many classes will be pointers, but for some classes, like CFString, a CFStringRef will already be a pointer. Many CFString functions return as a CFStringRef. CFString * and CFStringRef are interchangeable with NSString * (this is called toll-free bridging), although the compiler will probably appreciate it if you cast it first.

CFString *veryStringyString = @"STRINGYNESS!!11!one";
CFStringRef especiallyStringyString = @"STRRRRRRINNNNNGGGGYYYYY";
NSString *STRINGYNESS = (NSString *)veryStringyString;

Upvotes: 1

stefanB
stefanB

Reputation: 79780

Pointers are used to hold address of allocated memory. When you create object in cocoa you allocate memory and store the address in pointer.

BOOL, char, int store the value.

When you create a class the alloc allocates memory so you need to store a pointer to that memory to be able to access it:

NSMutableArray * arr = [[NSMutableArray alloc] init];

How do the C types get cleaned up from memory?

'Simple' types are allocated on stack. When a method is called a space is allocated on stack to hold all method variables (plus some other stuff like parameters and return address and so on). So stack grows. Once method returns the stack shrinks and the space that was used by method is now reclaimed - so yes the simple types will get 'cleaned up'.

It's actually much simpler than it sounds. Have a look at wikipedia Stack entry - section Hardware stacks for more details to satisfy your curiosity.

When you 'allocate memory' that memory is allocated on heap. Heap is there for the whole execution of your application. Once you allocate memory on a heap you get an address to that memory - you store this address in pointers. Pointer is just a variable that stores memory address.

When your method returns you no longer have access to your 'simple' variables declared within method (e.g. BOOL, int, char and so on) but the memory on heap is still there. Provided you still have the address of the memory (e.g. the pointer) you can access it.

What about instance variables of 'simple' type (edit: inside object?) ?

When you create object (we're talking about objective C and Cocoa here) and alloc it you allocate space for the whole object. The size of object is the size of all it's variables (not sure if obj-c adds other stuff). So the instance variables are part of your object memory on heap. When you release/delete object its memory is reclaimed and you no longer have access to variables that are stored inside object (in obj-c you call release, each object keeps reference count, when the reference count hits 0 the object is deallocated - the memory on heap is reclaimed).

Upvotes: 5

Andrew Pouliot
Andrew Pouliot

Reputation: 5421

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.

Upvotes: 20

Related Questions