Take the following example:
class A { var num: Int required init(num: Int) { self.num = num } } class B: A { func haveFun() { println("Woo hoo!") } }
I've marked A
's init
function as required
. What exactly does this mean? I completely omitted it in the subclass B
and the compiler doesn't complain at all. How is it required, then?
An initializer is a special type of function that is used to create an object of a class or struct. In Swift, we use the init() method to create an initializer. For example, class Wall { ... // create an initializer init() { // perform initialization ... } } Here, the method init() is an initializer of the class Wall .
Swift init() Initialization is the process of preparing an instance of a class, structure, or enumeration for use. This process involves setting an initial value for each stored property on that instance and performing any other setup or initialization that is required before the new instance is ready for use.
Convenience initializers are secondary, supporting initializers for a class. You can define a convenience initializer to call a designated initializer from the same class as the convenience initializer with some of the designated initializer's parameters set to default values.
An open class member is accessible and overridable outside of the defining module. A public class is accessible but not subclassable outside of the defining module. A public class member is accessible but not overridable outside of the defining module.
See "Automatic Initializer Inheritance":
Rule 1 If your subclass doesn’t define any designated initializers, it automatically inherits all of its superclass designated initializers.
Rule 2 If your subclass provides an implementation of all of its superclass designated initializers—either by inheriting them as per rule 1, or by providing a custom implementation as part of its definition—then it automatically inherits all of the superclass convenience initializers.
In your example, the subclass B
does not define any initializers on its own, therefore it inherits all initializers from A
, including the required initializer. The same is true if B
defines only convenience initializers (now updated for Swift 2):
class B: A { convenience init(str : String) { self.init(num: Int(str)!) } func haveFun() { print("Woo hoo!") } }
But if the subclass defines any designated (= non-convenience) initializer then it does not inherit the superclass initializers anymore. In particular the required initializer is not inherited, so this does not compile:
class C: A { init(str : String) { super.init(num: Int(str)!) } func haveFun() { print("Woo hoo!") } } // error: 'required' initializer 'init(num:)' must be provided by subclass of 'A'
If you remove the required
from A
's init method then class C
compiles as well.
The required
keyword means that inheriting classes must provide an implementation of the method. However, the language makes an exception for required initializers:
You do not have to provide an explicit implementation of a required initializer if you can satisfy the requirement with an inherited initializer.
Reference to the documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With