Swift Classes Require Child Initializers to Call super.init() After Own Setup
In Swift, when a child class defines its own initializer, it must explicitly call the parent class's initializer using super.init() to ensure all inherited properties receive values. Swift enforces a strict ordering rule: a subclass must initialize its own properties before calling super.init(), not after. This requirement exists because every property in a Swift class must have a valid value by the time initialization completes, and the parent class is responsible for setting up its own stored properties. Skipping or reordering the super.init() call causes a compile-time error, preventing unsafe or partially initialized objects. Understanding this two-step pattern — set your own properties first, then delegate to the parent — is essential for working correctly with class inheritance in Swift.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in