Why this happens
Swift enforces a strict two-phase initialization process to guarantee that every stored property has a valid value before the instance is used. The compiler performs four safety checks, and the first one states: a designated initializer must ensure that all properties introduced by its class are initialized before it delegates up to a superclass initializer.
In the example, Square introduces sideLength. When super.init(name:name) is called before self.sideLength = sideLength, the compiler sees that sideLength is still uninitialized at the point of delegation. This violates safety check 1, so the build fails with:
property 'self.sideLength' not initialized at super.init call
This differs from Objective-C, where [super init] is typically called first. Swift’s design prevents partially initialized objects from escaping into the superclass initializer, where they could be accessed by overridden methods or passed to other code.
How to fix it
1. Initialize subclass properties before calling super.init()
The direct fix is to assign every stored property declared in the subclass before the super.init() call.
class Shape {
var numberOfSides = 0
var name: String
init(name: String) {
self.name = name
}
func simpleDescription() -> String {
return "A shape with \(numberOfSides) sides."
}
}
class Square: Shape {
var sideLength: Double
init(sideLength: Double, name: String) {
// 1. Initialize all properties introduced by Square
self.sideLength = sideLength
// 2. Then delegate to superclass
super.init(name: name)
// 3. Optional further setup (mutating inherited properties, etc.)
numberOfSides = 4
}
func area() -> Double {
return sideLength * sideLength
}
}
Why this works: After self.sideLength = sideLength, every property that Square owns has a value. The instance is now in a consistent state for the superclass initializer to run safely.
2. Use a convenience initializer when you need superclass setup first
If you genuinely need the superclass to be partially configured before you can compute a subclass property, factor the logic into a convenience initializer that calls a designated initializer after the superclass is ready.
class Square: Shape {
var sideLength: Double
// Designated initializer – follows the rule
init(sideLength: Double, name: String) {
self.sideLength = sideLength
super.init(name: name)
numberOfSides = 4
}
// Convenience initializer – can call super.init first
convenience init(name: String, area: Double) {
let side = sqrt(area)
self.init(sideLength: side, name: name) // delegates to designated
}
}
Convenience initializers must delegate to another initializer in the same class (designated or another convenience) before the instance is fully initialized. This keeps the safety checks intact while giving you flexibility.
3. Provide default values to avoid explicit initialization
If a property can have a sensible default, declare it with an initial value. The compiler then considers it initialized before any initializer body runs.
class Square: Shape {
var sideLength: Double = 0 // default value → already initialized
init(sideLength: Double, name: String) {
self.sideLength = sideLength // still assign the argument
super.init(name: name)
numberOfSides = 4
}
}
This doesn’t eliminate the assignment, but it satisfies the compiler’s requirement that the property has a value before super.init().
How to prevent it
- List subclass properties first in the initializer body, then call
super.init(). - Use default values for properties that don’t depend on initializer parameters.
- Prefer designated initializers for the primary setup path; add convenience initializers only when you need alternative parameter combinations.
- Run the Swift compiler with
-warn-uninitialized(enabled by default) to catch missing initializations early. - Review the initialization chapter in The Swift Programming Language whenever you add a new subclass.
FAQ
### Can I call a method on self before super.init()?
No. Until all subclass properties are initialized and super.init() completes, self is not fully formed. Calling instance methods (including property observers) before delegation is a compile-time error.
### What if the superclass initializer needs a value that depends on a subclass property?
Restructure the API: compute the needed value before the initializer chain starts, pass it as an argument, or use a factory method / convenience initializer that performs the calculation and then delegates to the designated initializer.
### Does this rule apply to structs and enums?
Structs and enums don’t have inheritance, so there is no super.init(). However, every stored property must still be initialized before the initializer finishes. The same “initialize before use” principle applies, but without the delegation step.
Parts of this guide are adapted from content licensed under CC BY-SA 3.0; this article is shared under the same license.
