Skip to content

Instantly share code, notes, and snippets.

@JeOam
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeOam/9116926 to your computer and use it in GitHub Desktop.
Save JeOam/9116926 to your computer and use it in GitHub Desktop.
Answer to a stackoverflow question.
@JeOam
Copy link
Author

JeOam commented Feb 20, 2014

Multiple initializers
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.
Multiple initializers

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.

via: https://developer.apple.com/library/ios/documentation/general/conceptual/devpedia-cocoacore/MultipleInitializers.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment