Can someone explain to me what init and alloc do in Obj-C. I am reading this obj-c book that gives an example of creating object but it does not really go into details of wh
The NSObject root class provides a class method, alloc, alloc: reservers a memory location and returns the pointer to that memory location.
The alloc method has one other important task, which is to clear out the memory allocated for the object’s properties by setting them to zero.
This avoids the usual problem of memory containing garbage from whatever was stored before, but is
not enough to initialize an object completely.
You need to combine a call to alloc with a call to init, another NSObject method:
(id)init; The init method is used by a class to make sure its properties have suitable initial values at creation. The correct way to allocate and initialize an object is to nest the alloc call inside the call to init, like this:
ClassName *objectName = [ClassName alloc] init];
from: apple documents