Syntax and Object Allocation:


Coming from the world of Java and Android programming, I'm used to initializing an object in the following format:


Coffee cupOfJoe = new Coffee;


In Objective-C (which is actually a superset of the C language), the same line is written like so:


Coffee *cupOfJoe = [Coffee new];


However, the recommended format is this (which is equivalent to the above):


Coffee *cupOfJoe = [[Coffee alloc] init];


The reason for this is that we have more functionality with this latter method.  There are variations of the init function such as initWithString, etc. This format is also followed for historical reasons (just crack open any supporting libraries to prove it to yourself).




No garbage collection:


Unlike Android (which comes complete with garbage collection since it is programmed in Java), we need to release our objects after we are done using them. 


[cupOfJoe release];


We can also edit the dealloc method in the implementation file. This typically is not recommended. It is inherited from our super class, but we can add to its functionality. 


-(void) dealloc {
    NSLog(@"The world is about to explode!");
    [super dealloc]
}


The NSLog addition will run just before the object will be deleted.


Although I can't confirm yet, I've heard this no garbage collection limitation doesn't exist when creating mac desktop applications-- just iPhone applications.




Autorelease Pools:


Typically, if we own an object, we are responsible for releasing it. However, this isn't always doable.


Sometimes we need an autorelease pool. 


NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];


Autorelease loosely translates to, "Release my object, but not immediately." This is useful for cases like below:


-coolFunction {
  A *a = [[A alloc] init];
  [a autorelease];  //imagine using "release" here like normal
  return a;
}


N *n = [[N alloc] init];
A *coolResult = [n coolFunction];


We have an object allocated inside the function, but it will be released before it can be returned! So instead of using release, we would use autorelease so that the object can be returned. This functionality shouldn't be abused.




As an interesting side note, the "NS" in front of NSObject or NSLog functions stand for "Next Step," from NeXT Computers (the Steve Jobs venture).