I was creating my own custom tableViewCell and then I got an error saying:
'required' initializer 'init(coder:)' must be provided by subclass of 'UITableViewCell'
I looked it up and obviously it's a must to implement that as well. But this led to my confusion about required vs. designated initializers
Apple Docs says:
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:
Designated initializers
Designated initializers are the primary initializers for a class. A designated initializer fully initializes all properties introduced by that class and calls an appropriate superclass initializer to continue the initialization process up the superclass chain.
Are the following statements correct:
- A required initializer is always a designated initializer
- Every designated initializer is not necessarily a required initializer
- A class can only have one required initializer, however it can have multiple designated initializers?
Having that said I still don't fully understand their functional differences.
Required initialisers and designated initialisers are not really related, though the associated keywords
required
andconvenience
are both used to specify restrictions on subclasses.Required Initialisers
A required initialiser makes a guarantee that you can initialise a type, or any of its sub-types, with that initialiser. If you have an initialiser in a protocol and you conform something to that protocol, you have to use
required
(if it's a class) because that protocol guarantees that the initialiser is present on that class, and any of its subclasses. When you userequired
on an initialiser of a class, that signals that all of its subclasses can also be initialised using that method. This means you also need to add that initialiser to any of its subclasses.Here, the
required
keyword must be present because any subclasses ofTestClass
must also provideinit()
(because they also conform toTestProtocol
).Having a required initialiser allows you to initialise a class without knowing what it is at compile time, which is useful for a variety of reasons:
If your class conformed to multiple protocols, each with a different initialiser for example, each of those initialisers must also be required:
Note that adding
super.init()
isn't required in this special case, because Swift will automatically include the call if it takes no parameters.In all the above examples, the initialisers are designated because they do not include the
convenience
keyword.Even if you didn't have any protocols, you can still make use of
required
by initialising a type of a class which isn't known at compile time:Designated Initialisers
A designated initialiser is one which isn't a convenience initialiser (i.e, marked with
convenience
). A designated initialiser must make sure that all properties of the class have a value before the initialiser finishes (or a super initialiser is called). Convenience initialisers only don't have this requirement because they must themselves call a designated initialiser.(This is fairly contrived example.)
In my experience, convenience initialisers are rarely useful and I tend to find the problems they solve can be solved using optional arguments on designated initialisers instead. One also needs to consider the fact that initialisers can't call convenience initialisers on their superclass, so make sure you don't have any convenience initialisers which provide functionality that your designated initialisers don't if you intend your class to be subclassed!
Structs and enums don't use the
required
orconvenience
keywords because these keywords are both used to indicate initialisation rules for subclasses, which onlyclass
es support: Therequired
keyword indicates that subclasses must provide that initialiser, and theconvenience
keyword indicates that subclasses cannot call that initialiser. Despite not having the keywords, they must still provide initialisers defined in any protocols they conform to, and you can write 'convenient' initialisers which callself.init
, just without theconvenience
keyword.To respond to your statements: