I’ve made my way through almost the entire book at this point, and a very simple question remains unanswered: When do I use alloc?
Now, the answer may seem obvious to some, but I’m afraid I am still unsure. I know that whenever I want to send a message to an object, I must allocate memory and initialize that instance of that object. Fine, got it. So why don’t we just send
[[someObject alloc] init]; to every object we send a message to?
It is my understanding that this is because some objects are sent an implicit autorelease message. For example:
NSString *aString = [NSString stringWithFormat:@"format"];
This creates the pointer for our aString variable and autoreleases it.
Ok, well maybe we should only alloc init objects that we create (i.e. Possession, TouchDrawView, HTTPServer). Again, not the case:
[[NSURLConnection alloc] initWithRequest:req delegate:self];
So, I am left wondering how to know whether to alloc init an object, to alloc initWithSomeOtherFormOfInit the object, or let Objective-C create an autoreleased instance of the object. Is there no hard and fast rule, or did I miss something around page 4 and just never pick up on it? Help is appreciated.