Swift
initialization
convenience init
init method
programming examples

What is the difference between convenience init vs init in swift, explicit examples better

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Swift classes, init usually means a designated initializer, while convenience init means a secondary initializer that delegates to another initializer in the same class. The distinction matters because Swift enforces explicit initialization rules for inheritance. Once you understand which initializer is responsible for creating the full stored state, the difference becomes much easier to remember.

Designated Initializers Do the Real Setup

A designated initializer is the initializer that makes sure the class’s stored properties are initialized and that superclass initialization is completed.

swift
1class Vehicle {
2    let wheels: Int
3
4    init(wheels: Int) {
5        self.wheels = wheels
6    }
7}
8
9class Car: Vehicle {
10    let model: String
11
12    init(model: String) {
13        self.model = model
14        super.init(wheels: 4)
15    }
16}
17
18let car = Car(model: "Sedan")
19print(car.model)
20print(car.wheels)

Here Car.init(model:) is designated because it initializes model, the property introduced by Car, and then calls a designated initializer on the superclass.

A designated initializer always delegates upward with super.init(...) once the subclass has initialized its own stored properties.

Convenience Initializers Are Shortcuts

A convenience initializer is a secondary entry point. It does not finish object setup on its own. Instead, it must call another initializer from the same class.

swift
1class Car: Vehicle {
2    let model: String
3
4    init(model: String) {
5        self.model = model
6        super.init(wheels: 4)
7    }
8
9    convenience init() {
10        self.init(model: "Default")
11    }
12}
13
14let defaultCar = Car()
15print(defaultCar.model)

convenience init() is a helper. It provides an easier way to create a Car, but it still relies on the designated initializer to do the actual setup.

That is the core difference: designated initializers build the object, convenience initializers route callers to a designated initializer.

The Delegation Rules Matter

Swift enforces three simple rules for class initialization:

  • a designated initializer must call a designated initializer from its superclass
  • a convenience initializer must call another initializer from the same class
  • eventually, every initialization path must reach a designated initializer

Those rules prevent half-initialized objects and make inheritance behavior predictable.

Why Structures Do Not Use convenience

This distinction applies to classes, not structs. Structures do not have class inheritance, so they do not need the same designated-versus-convenience model.

swift
1struct Point {
2    let x: Int
3    let y: Int
4
5    init(x: Int, y: Int) {
6        self.x = x
7        self.y = y
8    }
9
10    init() {
11        self.init(x: 0, y: 0)
12    }
13}

This looks similar to a convenience initializer, but in a struct it is just another initializer. There is no convenience keyword because there is no superclass chain to manage.

When to Use Each One

Use a designated initializer when the initializer is responsible for creating the real stored state of the class.

Use a convenience initializer when you want:

  • default values
  • a shorter calling style
  • a helper path that translates one set of parameters into another

A convenience initializer is a wrapper. A designated initializer is the actual constructor path that guarantees the object is fully initialized.

Common Pitfalls

  • Assuming every init in a class is automatically just a simple constructor with no delegation rules.
  • Trying to call super.init directly from a convenience initializer.
  • Using convenience init when the initializer actually needs to initialize new stored properties.
  • Forgetting that structs do not use the same designated-versus-convenience distinction.
  • Writing several overlapping initializers without one clear designated path that owns the real setup.

Summary

  • In classes, a designated initializer performs the real object setup and delegates upward to the superclass.
  • A convenience initializer is a helper that delegates across to another initializer in the same class.
  • Every initialization path must eventually reach a designated initializer.
  • 'convenience init is mainly for shortcuts, defaults, and alternate argument forms.'
  • Structs can have multiple initializers too, but they do not use convenience because they do not inherit from superclasses.

Course illustration
Course illustration

All Rights Reserved.