When you override
an initializer such as init(frame: CGRect)
, you do it against a designated initializer, i.e. the principal initializer responsible for creating an object programmatically. According to rules, you must call super.init
before performing any additional init actions.
Required initializers, on the other hand, are not the same as designated.
Required Initializers
Write the required modifier before the definition of a class
initializer to indicate that every subclass of the class must
implement that initializer.
You must also write the required modifier before every subclass
implementation of a required initializer, to indicate that the
initializer requirement applies to further subclasses in the chain.
You do not write the override modifier when overriding a required
designated initializer.
Source: Apple Documentation On Initializers
You implement required initializer regardless of your will when you make a subclass if original class has that initializer marked as required. Commonly, you are not supposed to call it directly to create an object, although in certain cases you do. Required initializers are marked required to comply with several, well, requirements. For instance,
required init?(coder: NSCoder)
is called in several cases. One case: when the view is created from IB. Another case - when an object is created, or rather, unarchived (that is called deserialization) manually. In fact, when you put that object onto a ViewController's view (for example), that object is also unarchived, i.e. gets all its properties loaded and set.
Obviously, when you make a subclass of some view, you are supposed to make it support this interface, which provides deserialization capability (in case it is sometime used from Interface Builder or either). This is why it is required
.