Objects Are Created Dynamically
Memory is allocated dynamically for an Objective-C object. The first step in creating an object is to make sure enough memory is allocated not only for the properties defined by an object’s class, but also the properties defined on each of the superclasses in its inheritance chain.
The NSObject
root class provides a class method, alloc
, which handles this process for you:
+ (id)alloc; |
Notice that the return type of this method is id
. This is a special keyword used in Objective-C to mean “some kind of object.” It is a pointer to an object, like (NSObject *)
, but is special in that it doesn’t use an asterisk.
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, and is covered in more detail in the next chapter.
Note that init
also returns an id
.
If one method returns an object pointer, it’s possible to nest the call to that method as the receiver in a call to another method, thereby combining multiple message calls in one statement. The correct way to allocate and initialize an object is to nest the alloc
call inside the call to init
, like this:
NSObject *newObject = [[NSObject alloc] init]; |
This example sets the newObject
variable to point to a newly created NSObject
instance.
The innermost call is carried out first, so the NSObject
class is sent the alloc
method, which returns a newly allocated NSObject
instance. This returned object is then used as the receiver of the init
message, which itself returns the object back to be assigned to the newObject
pointer, as shown in Figure 2-5.

Initializer Methods Can Take Arguments
Some objects need to be initialized with required values. An NSNumber
object, for example, must be created with the numeric value it needs to represent.
The NSNumber
class defines several initializers, including:
- (id)initWithBool:(BOOL)value; |
- (id)initWithFloat:(float)value; |
- (id)initWithInt:(int)value; |
- (id)initWithLong:(long)value; |
Initialization methods with arguments are called in just the same way as plain init
methods—an NSNumber
object is allocated and initialized like this:
NSNumber *magicNumber = [[NSNumber alloc] initWithInt:42]; |
Class Factory Methods Are an Alternative to Allocation and Initialization
As mentioned in the previous chapter, a class can also define factory methods. Factory methods offer an alternative to the traditional alloc] init]
process, without the need to nest two methods.
The NSNumber
class defines several class factory methods to match its initializers, including:
+ (NSNumber *)numberWithBool:(BOOL)value; |
+ (NSNumber *)numberWithFloat:(float)value; |
+ (NSNumber *)numberWithInt:(int)value; |
+ (NSNumber *)numberWithLong:(long)value; |
A factory method is used like this:
NSNumber *magicNumber = [NSNumber numberWithInt:42]; |
This is effectively the same as the previous example using alloc] initWithInt:]
. Class factory methods usually just call straight through to alloc
and the relevant init
method, and are provided for convenience.
Use new to Create an Object If No Arguments Are Needed for Initialization
It’s also possible to create an instance of a class using the new
class method. This method is provided by NSObject
and doesn’t need to be overridden in your own subclasses.
It’s effectively the same as calling alloc
and init
with no arguments:
XYZObject *object = [XYZObject new]; |
// is effectively the same as: |
XYZObject *object = [[XYZObject alloc] init]; |
Literals Offer a Concise Object-Creation Syntax
Some classes allow you to use a more concise, literal syntax to create instances.
You can create an NSString
instance, for example, using a special literal notation, like this:
NSString *someString = @"Hello, World!"; |
This is effectively the same as allocating and initializing an NSString
or using one of its class factory methods:
NSString *someString = [NSString stringWithCString:"Hello, World!" |
encoding:NSUTF8StringEncoding]; |
The NSNumber
class also allows a variety of literals:
NSNumber *myBOOL = @YES; |
NSNumber *myFloat = @3.14f; |
NSNumber *myInt = @42; |
NSNumber *myLong = @42L; |
Again, each of these examples is effectively the same as using the relevant initializer or a class factory method.
You can also create an NSNumber
using a boxed expression, like this:
NSNumber *myInt = @(84 / 2); |
In this case, the expression is evaluated, and an NSNumber
instance created with the result.
Objective-C also supports literals to create immutable NSArray
and NSDictionary
objects;