Property Observers

A portrait painting style image of a pirate holding an iPhone.

by The Captain

on
June 7, 2023

Using Property Observers in Swift

Property observers allow you to observe and respond to changes in property values. Swift provides two types of property observers: willSet and didSet. The willSet observer is called just before the value of the property is about to change. The didSet observer is called immediately after the value of the property has changed.

Let's define a class with a property that has property observers:

class Person {
    var name: String = "" {
        willSet {
            print("About to set name to: \(newValue)")
        }
        
        didSet {
            print("Name was set to: \(name)")
        }
    }
}

In this example, we have defined a class Person with a property name. The name property has two property observers: willSet and didSet.

Let's create an instance of the Person class and set the name property:

let person = Person()
person.name = "John"}

The output of the above code will be:

About to set name to: John
Name was set to: John}

As you can see, the willSet observer is called just before the value of the name property is about to change and the didSet observer is called immediately after the value of the name property has changed.

Using Property Observers with Computed Properties

You can also use property observers with computed properties. In this case, the willSet observer is called just before the computed property is about to be calculated and the didSet observer is called immediately after the computed property has been calculated.

Let's define a class with a computed property that has property observers:

class Rectangle {
    var width: Double = 0
    var height: Double = 0
    
    var area: Double {
        willSet {
            print("About to calculate area with width and height: \(width), \(height)")
        }
        
        didSet {
            print("Area was calculated with width and height: \(width), \(height)")
        }
    }
    
    init(width: Double, height: Double) {
        self.width = width
        self.height = height
        area = width * height
    }
}

In this example, we have defined a class Rectangle with two properties: width and height. We have also defined a computed property area that depends on the values of the width and height properties. The area property has two property observers: willSet and didSet.

Let's create an instance of the Rectangle class and update the width and height properties:

let rectangle = Rectangle(width: 4, height: 5)
rectangle.width = 6
rectangle.height = 7}

The output of the above code will be:

About to calculate area with width and height: 4.0, 5.0
Area was calculated with width and height: 4.0, 5.0
About to calculate area with width and height: 6.0, 5.0
Area was calculated with width and height: 6.0, 5.0
About to calculate area with width and height: 6.0, 7.0
Area was calculated with width and height: 6.0, 7.0}

As you can see, the willSet observer is called just before the area property is about to be calculated and the didSet observer is called immediately after the area property has been calculated.

Summary

Property observers are a useful feature in Swift that allow you to observe and respond to changes in property values. The willSet observer is called just before the value of the property is about to change and the didSet observer is called immediately after the value of the property has changed. Property observers can also be used with computed properties, where the willSet observer is called just before the computed property is about to be calculated and the didSet observer is called immediately after the computed property has been calculated.