What are convenience required initializers in Swift? -
i saw in this answer user specifies convenience required init()
. mean?
i understand required
keyword used overriding superclass's designated initializer, convenience required
declarative do?
a convenience required initializer initializer enforced onto subclasses not designated initializer. means said initializer phone call designated initializer in initialization chain.
designated initialisersa designated initialiser canonical initializer class , 1 required , convenience initialisers should call. docs say:
designated initializers primary initializers class. designated initializer initializes properties introduced class , calls appropriate superclass initializer go on initialization process superclass chain.
convenience initialisersa convenience initialiser initializer sets configuration info on class...conveniently. documentation:
convenience initializers secondary, supporting initializers class. can define convenience initializer phone call designated initializer same class convenience initializer of designated initializer’s parameters set default values. can define convenience initializer create instance of class specific utilize case or input value type.
you not have provide convenience initializers if class not require them. create convenience initializers whenever shortcut mutual initialization pattern save time or create initialization of class clearer in intent
required initialisersrequired initializers can thought of binding contract between parents interface , subsequent subclasses. means of enforcing children aware of , implement set of initialisers.
write required modifier before definition of class initializer indicate every subclass of class must implement initializer:
swift
No comments:
Post a Comment