Variable & Object Types

BOOL

Values:- YES, NO

NSLog(@"The answer is %i", MyValue);

char

UInt8

int

32bits (currently unless the iPhone ever moves to a 64bit chipset).  If you want to future protect pointers for a 64bit system you can use NSInteger instead.

NSLog(@"Value = %i", MyValue);

Converting an int to an object:


	[MyArray addObject:[NSNumber numberWithInt:MyIntVariable]];

 

long long / unsigned long long

float

NSLog(@"Value = %f", MyValue);

double

NSLog(@"Value = %f", MyValue);

NSString

NSDate

NSObject

Most classes in Objective-C are derived from NSObject:


    NSObject *object;
    object = [[NSObject alloc] init];
    NSLog(@"object = %@", object);
    [object release];

id

You may be working with an object but you are not sure what that object will be when the code executes. The id variable can be used as a sort of placeholder for any object. It is often used in the Cocoa-Touch frameworks and it is one of the things that contributes to the powerful flexibility of the Objective-C programming language.


    id someObject;
    someObject = object;
    NSLog(@"some object = %@", someObject);

Currency

A good way is to use NSNumber and NSNumberFormatter together so that the currency will always be localized for whatever country the device is in.


	NSNumber *MyValue = [NSNumber numberWithFloat:29.99];
	NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
	numberFormatter.numberStyle = kCFNumberFormatterCurrencyStyle;
	NSLog(@"The cost is %@", [numberFormatter stringFromNumber:MyValue]);
	[numberFormatter release];

static

Use static as for normal C to declare static variables:


	static NSString *SomeStringName;

self (this)

self is the address of the object running the current method.  It's like 'this' in VC++.  Typically it's used so that an object can send a message to itself:


	[self SomeMethodName];

super

super is used when you want to send a message to the self, but in it's super class (instead of th method being looked for in the local class first):


	[super SomeMethodName];

Memory Management If not using the new Automatic Reference Counting

The iPhone had no garbage collector without Automatic Reference Counting. Therefore memory management and the need for programmers to clean up after themselves is very important.  Memory is accessed as an object (i.e. via pointers to objects in memory) and not as access to the raw RAM bytes, and C level malloc and free levels of management are not requried.

alloc is used when creating an object.  The required memory is automatically allocated on the heap.

dealloc is how the memory is returned to the heap, but on the iPhone you do not call this method.  Instead iOS keeps a count for each object of the number of owners it has.  Objects can have multiple owners (using 'alloc' then 'retain') and be passed between owners, but as soon as an object has zero owners the object automatically sends itself the dealloc message and is destroyed.  However you need to deal with ensuring this count returns to zero by releasing objects you create when they are no longer required.  If you don't memory leaks occur.  For instance say a function say creates an Object to pass to some other function.  The function that created it doesn't need it any more but if it still exists it will remain an owner (it incremented the Objects owner count when it created it so it needs to decrement it to release it).

'Build & Analyze', instead of 'Build' will attempt to highlight possible memory leaks in code, although it is not perfect.

Some basic rules:

If you send the message 'alloc' to a class you are responsible for releasing it.

If you send the message 'copy' to an instance you are responsible for releasing it (just as if you had allocated it).

In any method that starts with 'new' you are responsible for releasing the object returned.

You can asssume that an object created by any other means (like a convenience method) will be marked for autorelease and you therefore don't need to worry about it.

If an object wants to keep another object it didn't allocate it must must send the 'retain' message.

Get into the habit of always writing correct dealloc methods – even when not required (e.g. for controller objects that exist for the entire application) they won't do any harm and getting into the habit will help reduce mistakes.

autorelease

autorelease is used on an object so that iOS will remove you (the function where you use it) as an owner but not until any other methods have had a chance to use it and possibly become an owner themselves.   What actually happens is that the autorelease occurs in the background servicing loop after all current methods and events have been completed (i.e. after all current triggers, calls chains etc are done).

Using on an object that exists:

[MyObject1 autorelease];

Using when creating an object:

NSObject *MyObject1 = [[[NSObject alloc] init] autorelease];

Using as part of an instruction, for instance when returning from a function:

return [MyObject1 autorelease];

release

Release an object from memory now.  Example, also setting it to nill in case it is accidentally accessed again:

[MyObject1 release];

MyObject1 = nill;

retain

Retain ownership of an object, for example one passed to a function.  Example:

[AnObject1 retain];

Arrays

When you add an object to an array, it calls retain on that object. If you don't release your pointer to that object, it will be a leak. When you release the array, it will call release on all of the objects that it holds, (as it called retain previously).

 

Showing an objects retain count while debugging


	NSLog(@"%lu", (unsigned long)[SomeVariable retainCount]));		//Display variables retain count