views:

2900

answers:

3

Hello hello,

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,

+15  A: 

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.

Andrew Pouliot
Thanks for your answer.This leaves me asking now, how do the C types get cleaned up from memory? Is it done under the Objective-C hood?
Meroon
The object's instance variables are all contained within the object's memory. They will all disappear when the object's memory is freed. The difference with object-type variables is that they're pointers, which means the variable itself just points to a place in memory. Even if the pointer variable disappears, the memory it points to is still allocated.
Chuck
Don't forget that sometimes in the header files, Apple have set up typedefs for structures where you always refer to them with a pointer. In that case the name ends in "Ref". e.g. CGImageRef which is a typedef of CGImage*. When you see that you don't put your own * on the variable name.
U62
+2  A: 

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).

stefanB
Oh ok that maskes sense for local variables within the scope of the method. But what about instance variables that are 'simple' types?
Meroon
+1  A: 

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;
AriX