Fix - Property not initialized at super.init call

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.

Error Example

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.

Initialize Properties Before Super Call

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.

Initialize At Declaration

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.

Using Optional 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)
  }
}

Why Is There A Safety Check

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.

If you liked this post and want to learn more, check out The Complete iOS Developer Bootcamp. Speed up your learning curve - hundreds of students have already joined. Thanks for reading!

Eddy Chung

I teach iOS development on ZeroToAppStore.com.

Similar Posts