A class may define multiple initializer methods, either when it can take different forms of input or to provide default initialization values as a convenience to clients. If the class can take initialization data in different forms or from different sources, it can declare an initializer for each form or source. For example, the
NSString class has initializers for obtaining string data as arrays of Unicode characters or from URL resources (among others).
Multiple initializers can also be a programming convenience. A class can have a series of initializers that, at one end, allow the client to specify all initial values and, at the other end, supply most or all of these values as defaults. Clients of the class may later be able to substitute new values for the default values through accessor methods or properties.
Framework classes sometimes have multiple factory methods as well as multiple initializers. They are similar in that they may be a convenience or take initialization data in different forms. However, they allocate the returned object as well as initialize it.
The Designated Initializer
The initializer of a class that takes the full complement of initialization parameters is usually the designated initializer. The designated initializer of a subclass must invoke the designated initializer of its superclass by sending a message to
super. The convenience (or secondary) initializers—which can include
init—do not call
super. Instead they call (through a message to
self) the initializer in the series with the next most parameters, supplying a default value for the parameter not passed into it. The final initializer in this series is the designated initializer.