Learn Objective-C: Complex Initializers

The subject of initializer methods deserves a bit of special attention. Most importantly, in addition to the default initializer method that is inherited from NSObject, most classes also have other initializers that take certain arguments. All initializers, however, follow the same pattern. Let’s look at an example.

Convenience Initializers

Additional initializers are often known as convenience initializers because you can initialize the object and set some or all of its instance variables (and perhaps other values) in one method call. To begin, let’s take a look at some of NSString‘s initializers. These include initWithString:, which takes an NSString as an argument and creates a new string from that, initWithFormat:, which takes a formatted string and a corresponding number of arguments, and plugs in the proper values for each format specifier, and returns the completed string, and initWithContentsOfFile:encoding:error:, which creates a string out of any text file. The latter example is a perfect example of the convenience nature of these methods—doing the same with C library functions would be a lot more difficult. Note also that NSString has class methods that do the same thing (except that according to the memory management rules, the values are autoreleased).

If we were to create a Rectangle class, you might have an initializer like this:

  1. -(id)initWithWidth:(float)w andHeight:(float)h {
  2.     if(!(self = [super init]))
  3.         return nil;
  4.     self.width = w;
  5.     self.height = h;
  6.     return self;
  7. }

Not too much that’s new there. After the initialization, we assign proper values to our instance variables and return. Let’s add another initializer:

  1. -(id)initWithWidth:(float)w andArea:(float)a {
  2.     if(!(self = [super init]))
  3.         return nil;
  4.     self.width = w;
  5.     self.height = (a / w);
  6.     return self;
  7. }

Not much that’s new here. We’ve simply created another convenience initializer.

The Designated Initializer

The interesting bit comes when we decide to subclass our Rectangle class. Suppose we create a ColoredRectangle, or (and this is perhaps a bad example) ColoredCube. We would be adding new instance variables—at least a color ivar, and maybe a further depth measurement. Normally, the subclass would have to override all of those methods, and if the superclass added another initializer, that would mean that all subclasses would have to override that as well.

Fortunately, Cocoa has a solution—the designated initializer. Typically the one with the most arguments, all the parallel initializers call that one, and the subclasses all use the designated initializers in their calls to super. Choose an initializer to be the designated initializer, and follow through with it:

  1. // Rectangle's initializers
  2. -(id)initWithWidth:(float)w andHeight:(float)h {
  3.     if(!(self = [super init]))
  4.         return nil;
  5.     self.width = w;
  6.     self.height = h;
  7.     return self;
  8. }
  10. -(id)initWithWidth:(float)w andArea:(float)a {
  11.     if(!(self = [self initWithWidth:w andHeight:(a / w)]))
  12.         return nil;
  13.     return self;
  14. }
  16. // ColoredCube's initializers
  17. -(id)initWithWidth:(float)w andHeight:(float)h {
  18.     if(!(self = [super initWithWidth:w andHeight:h]))
  19.         return nil;
  20.     self.depth = 1.0;    // Default value
  21.     self.color = [UIColor redColor];    // Ditto
  22.     return self;
  23. }
  25. -(id)initWithWidth:(float)w height:(float)h depth:(float)d andColor:(UIColor *)c {
  26.     if(!(self = [super initWithWidth:w andHeight:h]))
  27.         return nil;
  28.     self.depth = d;
  29.     self.color = c;
  30.     return self;
  31. }

Note here that we only have to override the default initializer in the subclass, because all the other inherited initializers will then call on that overridden initializer. We can also add more initializers as necessary.

By creating a default initializer (note that there is not specific syntax to denote the designated initializer—it’s up to the programmer to determine which one is the designated and stick with it), it makes subclassing a lot easier.

This post is part of the Learn Objective-C in 24 Days course.

Author: Feifan Zhou

Scroll to Top