What is a Computed Property in Swift?

Click for: original source

Computed properties are part of a family of property types in Swift. Stored properties are the most common which save and return a stored value whereas computed ones are a bit different. By Antoine van der Lee.

A computed property, it’s all in the name, computes its property upon request. It can be a valuable addition to any of your types to add a new property value based on other conditions. This post explains how and when to use them, as well as the differences between a computed value and a method. A Computed Property provides a getter and an optional setter to indirectly access other properties and values. It can be used in several ways.

The article walks you through:

  • What is a Computed Property?
  • Using a Computed Property to disclose rich data
  • Computed Properties inside an extension
  • Overriding Computed Properties
  • When should I use a Computed Property?
  • Taking into account performance when defining properties
  • Computed Properties vs Methods in Swift: When to use which?

Computed Properties are part of the basics in Swift. They can be used in many different ways and it’s important to know the different use-cases. Always take performance into account and try to prevent unneeded calculations if possible. Good read!

[Read More]

Tags swiftlang programming app-development web-development oop code-refactoring