Xcode will you give you error message Error in Swift class: Property not initialized at super.init call
when you haven’t initialized all the properties introduced by your class before calling the superclass initializer.
Here’s an example that will produce the error:
class Vehicle {
var brand: String
init(brand: String) {
self.brand = brand
}
}
class Car: Vehicle {
var model: String
init(model: String, brand: String) {
super.init(brand: brand)
self.model = model
}
}
The above code will produce the error Property 'self.model' not initialized at super.init call
.
This error will also occur if you have not set the model
property at all, for example:
class Car: Vehicle {
var model: String
init(model: String, brand: String) {
super.init(brand: brand)
}
}
Let’s go over some of the ways to fix this problem.
Initializing the property before the super call will make sense in most cases:
class Car: Vehicle {
var model: String
init(model: String, brand: String) {
self.model = model // Moved this before the super call!
super.init(brand: brand)
}
}
This should be the best solution in most cases.
Another way to solve this problem is to initialize the property when you define it.
This is a good solution if you would like to set a default value:
class Car: Vehicle {
var model: String = "Tesla" // Setting a default value
init(model: String, brand: String) {
super.init(brand: brand)
}
}
You won’t see the error if you set a default value for all your properties.
Another way to solve the problem is to make your properties optional by using a question mark ?
.
This is a good solution if you don’t want to set values for your properties during initialization.
class Car: Vehicle {
var model: String? // Use a question mark to make this optional
init(model: String, brand: String) {
super.init(brand: brand)
}
}
The safety check seems to be pretty pointless, so why does Swift have it?
Let’s take a look at a slightly more complicated example, printing the brand in the init function:
class Vehicle {
var brand: String
init(brand: String) {
self.brand = brand
print(brand) // Added this print statement
}
}
class Car: Vehicle {
var model: String?
init(model: String, brand: String) {
super.init(brand: brand)
}
}
Car(model: "X", brand: "Tesla")
This code prints Tesla
.
Now what if we wanted to change to the code so that our Car
class changed brand in it’s init function:
class Vehicle {
var brand: String
init(brand: String) {
self.brand = brand
print(brand)
}
}
class Car: Vehicle {
var model: String?
init(model: String, brand: String) {
super.init(brand: brand)
brand = "Ford" // Change brand here
}
}
Car(model: "X", brand: "Tesla")
What would the behavior of this code be if Swift allowed us to compile? The code would print Tesla
still, even though we want the brand to be Ford
.
That’s why this safety check exists in Swift.
The Complete iOS App Development Bootcamp
Disclosure: This website may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission.