In Swift, properties are either stored properties or computed properties.
Getters and setters in Swift are the methods a computed property uses to either set or get a value on demand.
- A stored property stores a value for later use (for example, a variable that belongs to a class instance).
- A computed property does not store a value. Instead, it computes it only when requested.
Syntactically, accessing a computed property looks the same as accessing a stored property.
For instance, let’s access the computed property kilos
of a weight
instance:
weight.kilos
The point is that it runs a piece of code, even though it looks as if we just accessed a variable.
Working With Computed Properties in Swift
To create computed properties, Swift offers you a getter and (an optional) setter method to work with.
- A getter method is used to perform a computation when accessing the property.
- A setter method is an optional method. It can be used to modify a property that relates to the computed property.
To better understand what the setter method does, imagine you have a weight for which you can call weight.kilograms
and weight.pounds
. However, there’s a problem when updating the weight:
weight.pounds = 100
Now, the weight.kilograms
is no longer up to date, as only pounds
got updated.
This is where the setter method is useful. You can use it to modify a related property. With the help of a setter method, calling weight.pounds = 150
updates the weight.kilograms
automatically.
Example of Getters and Setters in Swift
Say you are working with weights and you want to easily convert between kilos and pounds.
Before writing any code, let’s visualize how the code should work:
let weight = Weight() // Initialize a weight weight.kilograms = 100 // Set the weight in kilos print(weight.pounds) // Kilos as pounds weight.pounds = 315 // Assign a new weight in pounds... print(weight.kilograms) // ...and also update kilos automatically
Simply put, you want to be able to assign a new weight in either kilos or pounds with a single assignment so that it updates both of the weights automatically.
To accomplish this, let’s utilize computed properties:
class Weight { var kilograms: Float = 0.0 var pounds: Float { get { return kilograms * 2.205 } set(newWeight) { kilograms = newWeight / 2.205 } } }
Let’s examine the code above a bit more closely. Weight
is a class and kilograms
is just a regular variable that belongs to the class instance.
The pounds
variable is where the “magic” happens:
pounds
is a computed property. Its value is computed only when requested.- The
get
block does the on-demand computation: When you callweight.pounds
, thekilograms
property is converted to pounds. - The
set
block is responsible for updating the kilograms when the weight is changed in pounds. Notice thenewWeight
parameter in the setter. It simply refers to the new weight you are assigning topounds
.
Now the code works just like you want it to:
let weight = Weight() weight.kilograms = 100 print(weight.pounds) // The getter method is invoked weight.pounds = 315 // The setter method is invoked print(weight.kilograms)
Output:
220.5 142.85715
Now you know what computed properties are and how to create one yourself.
When Should I Use Getters and Setters in Swift
As you may have noticed, nothing stops you from replacing a computed property with a method. There is no right or wrong answer as to when computed properties should or should not be used, but here are some suggestions:
A computed property is useful when:
- The property depends on other properties (just like in the weight example before).
- You’re defining the property inside an extension.
Pick a method over a computed property if the property:
- Needs arguments. A computed property cannot accept arguments.
- Needs to perform computationally heavy tasks.
- Throws errors.
Conclusion
In Swift, a computed property does not store a value but rather computes it on demand when trying to access it.
A common use case for a computed property is to derive a value from another related property (e.g. kilograms to pounds).
When working with computed properties in Swift, you can use a getter method to compute a property. An optional setter method can also be used to update related properties (e.g. weight.kilograms = 100
updates weight.pounds
automatically).