Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
The counterpart to allocation is initialization. Initialization takes a chunk of memory and gets it ready to become a productive member of society. init methods—that is, methods that do initialization—almost always return the object they're initializing. You can (and should) chain your allocs and initializations like this:
Car *car = [[Car alloc] init];
and not like this:
Car *car = [Car alloc];
[car init];
This chaining technique is important because an initialization method might return an object that's not the same as the one that was allocated. If you think that's pretty odd, you're right. But it can happen.
Why might a programmer want an init method to return a different object? If you recall the discussion on class clusters at the end of Chapter 8, you saw that classes like NSString and NSArray are really just false fronts for a whole lot of specialized classes. An init method can take arguments, so the method code gets a chance to look at the arguments and decide that another class of object would be more appropriate. For example, let's say a new string is being made from a very long string, or maybe from a string of Arabic characters. Based on this knowledge, the string initializer might decide to create an object of a different class, one better suited to the needs of the desired string, and return that instead of the original object.