Skip to content
DeveloperMemos

Swift's Property Observers: didSet and willSet in Action

Swift, Property Observers1 min read

Property observers are a powerful feature in Swift that allow you to observe and respond to changes in property values. By using the didSet and willSet observers, you can execute code before or after a property's value is set. This article will delve into the practical applications of these property observers with examples to demonstrate their usefulness.

The willSet Observer

The willSet observer is called just before the value of a property is updated. It provides an opportunity to take action based on the new value that is about to be assigned. Let's consider an example of a Person struct with a name property:

1struct Person {
2 var name: String {
3 willSet {
4 print("Changing name from \(name) to \(newValue)")
5 }
6 }
7}
8
9var person = Person(name: "John")
10person.name = "Jane"

When the name property of the person instance is set, the willSet observer is triggered. In this case, it prints a message indicating the change in the name. Running this code will output:

1Changing name from John to Jane

By utilizing the willSet observer, you can perform additional operations or validations before assigning a new value to a property. This allows for dynamic and controlled behavior during property assignments.

The didSet Observer

On the other hand, the didSet observer is called immediately after a property's value has been updated. It provides an opportunity to react to the new value of the property. Let's continue with our Person example and add a didSet observer to the name property:

1struct Person {
2 var name: String {
3 didSet {
4 print("Name changed to \(name)")
5 }
6 }
7}
8
9var person = Person(name: "John")
10person.name = "Jane"

When the name property is assigned a new value, the didSet observer is triggered. In this case, it prints a message indicating the change in the name. Running this code will output:

1Name changed to Jane

The didSet observer allows you to perform actions or updates that rely on the new value of the property. It can be especially useful for triggering UI updates, updating dependent variables, or performing any necessary clean-up tasks.

Combining didSet and willSet

In many cases, you may want to combine both didSet and willSet observers to handle property changes comprehensively. Continuing with our Person example, let's add both observers to the name property:

1struct Person {
2 var name: String {
3 willSet {
4 print("Changing name from \(name) to \(newValue)")
5 }
6 didSet {
7 print("Name changed to \(name)")
8 }
9 }
10}
11
12var person = Person(name: "John")
13person.name = "Jane"

By using both observers together, you gain more control and insight into the property changes. Running this code will output:

1Changing name from John to Jane
2Name changed to Jane

The willSet observer is called first, followed by the didSet observer. This order allows you to execute appropriate actions before and after updating the property value.


Swift's property observers, namely didSet and willSet, provide a convenient way to observe and respond to changes in property values. By leveraging these observers, you can add custom behavior before or after a property is updated. Whether it's performing validations, triggering UI updates, or managing dependent variables, property observers offer flexibility and control in Swift development.