Here are the rules:
- If you create an object by calling
alloc or copy, you own it and must release it when you're done.
- If you didn't create an object, but want it to ensure it sticks around before control returns to the run loop (or, to keep things simple, your method returns), send it a
retain message and then release it later when you're done.
- If you create an object and want to return it from your method, you are obligated to release it, but you don't want to destroy it before the caller gets a chance to see it. So you send it
autorelease instead, which puts it in the Autorelease Pool, which is emptied once control gets back to the program's event loop. If nobody else retains the object, it will be deallocated.
Regarding arrays, you are free to do something like this:
NSObject *threeObjects[3];
threeObjects[0] = @"a string";
threeObjects[1] = [NSNumber numberWithInt:2];
threeObjects[2] = someOtherObject;
Reasons to use NSArray anyway: