Swift Extension

In Swift, we can add new functionality to existing types. We can achieve this using an extension.

We use the extension keyword to declare an extension. For example,

// class definition 
class Temperature {
  ...
}

// extension of Temperature class
extension Temperature {

  // add new methods 
} 

Here, we have created an extension of the Temperature class using the extension keyword.

Now, inside the extension, we can add new functionality to Temperature.


Example: Swift Extension

// class definition
class Temperature {
  var celsius: Double = 0

  func setTemperature(celsius: Double) {
    self.celsius = celsius
    print("Celsius:", celsius)
  }
}

// declare an extension
extension Temperature {

  // add a new method to Temperature class
  func convert() {
    var fahrenheit = (celsius * 1.8) + 32
    print("Fahrenheit:", fahrenheit)
  }
}

// class initialization
let temp1 = Temperature()
temp1.setTemperature(celsius: 16)

// access extension method using class object
temp1.convert()

Output

Celsius: 16.0
Fahrenheit: 60.8

In the above example, we have created an extension of the Temperature class.

extension Temperature {

  // add a new method to Temperature class
  func convert() {
    var fahrenheit = (celsius * 1.8) + 32
    ...
  }

This extension adds the following features to Temperature:

  • convert() - a method that simply converts the temperature from celsius to fahrenheit.
  • fahrenheit - a variable declared inside convert() that stores the result of the conversion.

We have then created an object named temp1 of Temperature, and used it to access the method created inside the extension.

// access extension method using class object
temp1.convert()

Note: Properties defined inside the class (like celsius) can be used inside the extension too.


Computed Property In Extension

In Swift, we cannot add stored properties in extensions. For example,

extension Circle {
  // stored property
  var radius: Int // error code
}

However, Swift lets us add computed properties to an extension. For example,

extension Circle {

  // computed property
  var area: Double {
    ...
  }
}

Here, area is a computed property defined in the extension body.


Example: Computed Property In Extension

class Circle {
  var radius: Double = 0
}

extension Circle {
  // define computed property 
  var area: Double {
    return 3.14 * radius * radius
  }
}

let circle1 = Circle()
circle1.radius = 5
print("Area:", circle1.area)

Output

Area: 78.5
 

In the above example, we have created an extension of the Circle class, where we have defined a computed property named area.

This property calculates the area of the circle based on the value of radius.

var area: Double {
  return 3.14 * radius * radius
}

Protocol Extension

In Swift, we can also extend the protocols. For example,

// protocol definition
protocol Brake {
  func applyBrake()
}

// extend protocol
extension Brake {
  func applyBrake() {
    print("Brake Applied")
  }
}

// define class that conforms Brake
class Car: Brake {
  var speed: Int = 0
}

let car1 = Car()
car1.speed = 61
print("Speed:", car1.speed)

// access extended protocol
car1.applyBrake()

Output

Speed: 61
Brake Applied

In the above example, we have created the protocol Brake that defines the function applyBrake().

We have extended the Brake protocol and defined the body of the applyBrake() function inside it.

// extend protocol
extension Brake {
  func applyBrake() {
    print("Brake Applied")
  }
}

Now, since the Car class conforms to the Brake protocol

class Car: Brake {
  ...
}

we can access the extended protocol using the car1 object.

// access extended protocol
car1.applyBrake()
Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges