During my first two weeks of learning Objective-C, my use of initializers was limited to a few staple instances.

  • In NSMutableArrays: [[NSMutableArray alloc]init]
  • In NSDictionaries: [[NSDictionary alloc]init]

In the above cases, the container object, either a dictionary or array, is initialized and objects are added later as needed with another method. In some cases, we have the objects already and use something like this:

  • [[NSMutableArray alloc]initWithObjects: @”do”, @”re”, @”mi”, nil]]
  • [[NSDictionary alloc] initWithObjectsAndKeys@”value1″, @”key1″, @”value2″, @”key2″, nil];

That way, we could cut out the step of adding the objects later.

Near the end of week two, we were given a broader introduction to object-oriented programming and began to create custom class objects. Now that we have classes beyond NSArrays, NSDictionaries and NSStrings, like Person, Book and Blog, we, as developers, need to decide how those objects behave.

Why use a custom initializer?

Just like an NSDictionary has a handy method, initWithObjectsAndKeys, unique to its structure, we can create an initializer unique to its custom class.

A custom initializer is used to combine two (or more) steps 1) the initialization of an object and 2) the assignment of its properties. Depending on how many properties an object has, it could combine more than two steps.

Designated Initializer | Superior to all other inits

This is the initializer that contains the most properties of the custom class. It is used as a catch-all for known properties of a class and accepts arguments for each of those properties. This initializer overrides the designated initializer from the inherited class, also known as the super class.

Call the new designated initializer on super to set as designated for the custom class.

Screen Shot 2016-06-21 at 9.27.52 AM

Default Initializer | Basic

This is where you can set default values if you need to initialize an object without any properties.

Screen Shot 2016-06-20 at 11.38.06 PM.png

Secondary Initializers | Almost as powerful as the designated

These are also called convenience initializers. What’s the difference between a secondary and default initializer? The secondary initializer has arguments similar to the designated—it just has less of them. Unlike the default and designated, you can have as many convenience initializers as you need.

Screen Shot 2016-06-20 at 11.37.35 PM

Looking forward

I am curious to know the standard amount of secondary initializers for a custom object and also how often defaults are used once a designated is set. I found the resources below helpful exploring this topic further.


The Engineers at Twitter wrote a detailed How-To on Initializers in Objective-C and gave examples for best practices.

A novice iOS Developer makes a case for custom initializers in Objective-C and explores the syntax.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s