Creating Object Instances
One of the
principal functions of a class
object is to serve as a factory for creating new instances. When new
objects are created, memory is allocated and its instance variables
are initialized. This is accomplished by using the
alloc
method, defined by the
NSObject
class, as follows:
Song song = [song alloc];
The alloc
class method dynamically allocates
memory, sets the isa
variable to a pointer to the
class’s class object, sets all other variables to 0,
and then returns the new object instance. This takes care of the
system level tasks that need to be performed when an object is
created, but doesn’t allow the object to properly
initialize itself. To give an opportunity for object-specific
initialization, the NSObject
class provides the
init
instance method. To fully create an instance
of the Song
class, use the following code:
Song song = [[song alloc] init];
The init
method can be overridden in a subclass
to assign defaults to instance variables and to take care of other
tasks that need to be performed before an object is used.
Tip
You can call the alloc
and init
methods by using separate lines of code. However, since object
allocation and initialization are interlinked, calling both methods
with one line of code is good practice.
When you override the
init
method, the superclass’s
init
method (or designated initializer, as covered
in the next section) should always be called to ensure that the
superclass is initialized properly. Initialization methods should
also return self
, the object being initialized.
Example 1-6 shows an init
method
for the Song
class.
The code shown in Example 1-6 performs the following tasks:
Declares the
init
method, which returns an object of typeid
. The returned object is the newly initialized object.Calls the
init
method of the superclass (super
) to let it properly configure its state. Theself
variable is set to the return value of theinit
method because it might return a different instance than the one currently being worked with.Returns the object using the
self
variable.
Initialization methods return an object of type id
so an initialization method can actually return a different object of
a different type, if necessary. For example, if a class needs to
return a more specialized subtype to better take advantage of a
system’s runtime configuration, it can release the
object originally created, create a new one of the subtype, and
return it. This is why programs need to use the object returned by
the init
method and not the object returned by the
alloc
method, and why you should make sure that
self
is set to the init
method’s return value.
Tip
The ability for an initialization method to return a subtype allows
for a programming pattern known as class
clusters
. This allows for a large amount of
functionality to be exposed behind a small and easy to understand
public class definition. For example, there are many different string
classes that are represented by the public
NSString
class.
Designated initializers
A class can provide multiple initialization methods to allow varying levels of customization. When you have multiple initializers, only the designated initializer should call the superclass’ initializer method. All other initializers must call the designated initializer. This will ensure that your classes always behave properly.
For example, if an initWithTitle
: method is
defined for the Song
class, the more general
init
method would first need to be called to allow
proper initialization of both the Song
class and
its parent classes before proceeding with specific initialization.
Example 1-7 shows an example.
Get Cocoa in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.