Πέμπτη, 5 Απριλίου 2012

The Designated Initializer

From the Documentation:

The designated initializer is the method in each class that guarantees inherited instance variables are initialized (by sending a message to super to perform an inherited method). It’s also the method that does most of the work, and the one that other initialization methods in the same class invoke. It’s a Cocoa convention that the designated initializer is always the method that allows the most freedom to determine the character of a new instance (usually this is the one with the most parameters, but not always).

  • It’s important to know the designated initializer when defining a subclass
  • General principle: The designated initializer in a class must, through a message to super, invoke the designated initializer in a superclass.
From the Big Nerd Ranch Guide:
"A class has only one designated initializer method. If the class has other initializers, then the implementation of those initializers must call (directly or indirectly) the designated initializer. 
  • Thus, the designated initializer acts as a funnel-point.
  • When you create a class whose designated initializer has a different name than its superclass's designated initializer (as you did in Appliance and OwnedAppliance), you have a responsibility to document that in the header file. 
  • If a class has several initializers, only one should do the real work. That method is known as the designated initializer. All other initializers should call, either directly or indirectly, the designated initializer. 
  • The designated initializer will call the superclass's designated initializer before initializing its instance variables
  • If the designated initializer of your class has a different name than the designated initializer of its superclass, you must override the superclass's designated initializer so that it calls the new designated initializer.
  • if you have several initializers, clearly document which is the designated initializer in the header. 


Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου