Computed Property

Introduction

Welcome to Lesson 2 of Object Oriented Swift. You will learn how to create relationships between variables. You no longer have to manage two or more variables separately. You probably have seen get and set before. If you haven’t, don’t you worry. I will walk you through by placing your feet on mine.

Problem

Do we have to make functions all the time to calculate something?

Atrocious Code

Unrelated functions that take up many lines. The functions below are used to calculate a radius and diameter.

  1. func getDiameter(radius: Double) -> Double {
  2. return radius * 2
  3. }
  4. func getRadius(diameter: Double) -> Double {
  5. return diameter / 2
  6. }
  7. getDiameter(radius: 10) // return 20
  8. getRadius(diameter: 200) // return 100
  9. getRadius(diameter: 600) // return 300

The two functions are separate and there is no relationship . Computed Property will help you solve this problem.

Introducing Computed Property

You may create a variable that only can be read. It is also referred to as a gettable property.

Computed = calculated

Gettable Property

Create a only readable property.

  1. class NumberClass {
  2. var readableNumber: Double {
  3. get {
  4. // Complex logic
  5. return 777
  6. }
  7. }
  8. var readableNumbers: Double {
  9. return 789
  10. }
  11. }

If you attempt to modify the property, it will give you an error.

  1. readableNumber = 10 // Error
  2. readableNumbers = 20 // Error

Gettable and Settable Property

You may not only make a property gettable, but also settable by inserting set right after get. Let us create a relationship between radius and diameter.

  1. var radius: Double = 10
  2. var diameter: Double {
  3. get {
  4. return radius * 2
  5. }
  6. set {
  7. radius = newValue / 2
  8. }
  9. }

newValue refers to the newly set value of diameter. For example, when you set diameter to 100, newValue becomes 100.

  1. diameter = 50
  2. radius = 100
  3. diameter // 200

Instead of calling newValue, you may add a different name. In the example below, there is a relationship between area and side. Instead of calling newValue, let us use newArea.

  1. import Foundation
  2. var side: Double = 100
  3. var area: Double {
  4. get {
  5. return side * side
  6. }
  7. set(newArea) {
  8. side = sqrt(newArea)
  9. }
  10. }

There is a relationship between side and area.

  1. area = 1000
  2. side // 31.6

Applications

  • Grabbing and storing data the server
  • Propagating relationships between properties
  • Preventing others from manipulating your code

Source Code

2002_computed_property.playground

Conclusion

You’ve learned what it means to create a gettable property that can be only read. You’ve also learned how to create a settable property which allows you to modify its value as well as those of others. The UIKIt framework often utilizes gettable properties to prevent us, developers, from modifying them.

In the lesson lesson, you will learn how to add an observer to a property.

Note: Learn Swift with Bob is available on Udemy. If you wish to receive a discount link, you may sign up here.