Swift computed properties

In this first post I will introduce Computed properties and how to use Getters and Setters, as an attempt to put on paper what I learn while i go through the Swiftlang features.

Computed properties

There are a few important things which are useful to know about before jumping in:

  • Think of them as pieces of code which will calculate a value every time you call them
  • Computed properties are always variables (never constants)
  • These type of properties can be used in or outside classes, structs, enums
Beware when using computed properties as their value might change each time you are calling them. Also, they can change any other value in their enclosing scope if a setter is used (more below)!!!

Defining a computed property

To start, you have to write a variable and explicitly declare the type of the property to help the compiler know what kind of value will be assigned to it.

Do not assign a default value. Instead open a bracket after the type declaration and start working on the getter.

As the value of the variable is set and retrieved through the use of setters and getters, you need to at least use the getter inside the brackets as below:

What happens in the code above:

  • You can see a computed property of type Int, and inside its brackets I am using a getter to return the result of x+y
  • Once the computed property is called the getter gets executed and the variable is assigned the result
  • A cleaning up note: you don’t need to write get { } as long as you do not implement a setter. This will speed you the writing

Let’s talk a bit about getters and setters:

Using get { }

  • The get keyword makes the computed property readable
  • A getter allows you to read data from the computed property
  • Inside the brackets you write the code that will be executed once the computed property is called
  • Accessing the property executes the getter
Try to assign a value to the computed variable. What happened?
You cannot assign a value to a computed property that doesn’t have a setter.

Using set { }

A few things to know:

  • The set keyword makes the computed property writeable. Without it you get this error: Cannot assign to value [your computed property’s name], it is a get-only property.
  • When you assign a value to the computed property you are calling the setter
  • The value is accessible inside the setter’s brackets
  • You can assign a local name inside the parentheses after the set keyword. This local variable will get the passed value to the computed property (as seen in the example below with newSalaryPerWeek)
  • If you do not provide a name for the assigned value in the setter definition, the compiler will automatically allocate the name of newValue.
  • Through set you can change the value of other properties in the same scope, reason why you have to be aware of all the dependencies linked to the computed property.

A bit confusing , but it’s a good example of the computed property power, as you can see below:

Why don’t I use the setter to change the value of its own computed property?
Because I would get into an infinite loop of assigning the value to itself over and over again. Try this at home :)

Let’s apply this knowledge to several other computed properties, but this time inside Enums/Structs/Classes, just to see that nothing changes:

  1. Computed properties in Enums

Below you can find two examples of working with a computed property in an Enum:

  • The first examples has a computed property that uses a switch statement to switch between the cases available in the Enum and return a String based on the selected case in that specific instance
  • The second example defines a read only computed property (because it only has a getter) which returns in this instance a specific String when called.
  • As these computed properties do not have a setter the get keyword can be omitted (saves time with typing).

2. Computed properties in Structs

Below we have the same example of a computed property, but this time inside a structure called Employee.

3. Computed properties in Classes

The example below uses a class with 2 stored properties and one computed property.

The computed property returns the area of the rectangle based on the stored property of width and height, however it gives us the possibility through the setter to change the values of width and height based on our input to the area of the rectangle, as can be seen below.

As this is quite important, I am mentioning again that computed properties can change any other values in their scope. This can create a lot of headaches.

Why are computed properties useful? Still trying to figure that out …

This is it for now. Part 2 will cover property observers.


  • Swift programming: The Big Nerd Ranch Guide
  • Learning Swift 3 — O’Reilly
Like what you read? Give Cosmin Mircea a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.