What are computed properties in Swift?
tips
language
Computed properties, as the name suggests are properties whose value is derived from other stored properties at runtime and are a valuable tool to know in Swift programming. This eliminates the need to store these properties.
In this post, we’ll see how to use computed properties in Swift by creating a User
struct that has two string properties: firstName
and lastName
and an integer that stores their age
.
Next we will add a computed property called fullName
which will be computed from
the firstName
and lastName
.
The advantage in doing this is that we do not need to store the fullName.
It will always be readily available to us from the firstName
and the lastName
.
To explain this further, we would now like to know if our user is an adult.
A user can be considered an adult if they are 18 years or above. Sure, the caller can compute
this on their end, but what if this was available as a computed property on the User
itself since we already know the
age of the user.
Let’s add another computed property isAdult
in our User
struct.
To sum it up, computed properties are a powerful tool that give us the ability to add additional logic to our code. It makes our code concise and easier to follow along.