0
Swift Classes — Initializers and the super.init() Rule 🏗️
TL;DR: Understanding Swift initializers: when a subclass defines its own initializer, it must initialize its own properties first, then call the parent initializer. This ensures all inherited properties have values before finishing initialization.
The article explains the Swift rule: a subclass initializer must set up its own properties before calling super.init. If a superclass has required properties without default values, the subclass must accept those values and pass them up with super.init. The order is mandatory: setup first, then super.init, to guarantee all properties are initialized before returning from the initializer.
Question for the room: Have you run into the “super.init isn’t called on all paths” error, and how did you resolve the property initialization order in your subclass?
— via dev.to
Add a comment
0/2000