Swift Properties Demystified: Understanding Stored and Computed Properties for Better iOS Development

sandeep kumar
3 min readAug 2, 2023
Stored VS Computed
Stored VS Computes

When working with Swift, properties are an essential part of defining the state and behaviour of your classes, structs, and enums. In this blog post, we’ll explore two types of properties in Swift: stored properties and computed properties. Understanding the differences between these types of properties will empower you to write more efficient and flexible code in your iOS app development journey.

Stored Properties

Stored properties are like regular variables in Swift that store values directly. They belong to an instance of a class, struct, or enum, and you can assign default values to them. Let’s take a simple example of a `Person` class to understand stored properties better:

class Person {
var name: String
var age: Int

init(name: String, age: Int) {
self.name = name
self.age = age
}
}

In this example, `name` and `age` are stored properties of the `Person` class. When you create an instance of the `Person` class, memory is allocated for these properties to store their respective values.

Computed Properties

Computed properties, on the other hand, don’t store values directly. Instead, they provide a getter and an optional setter to indirectly retrieve and set values based on other properties or custom logic. Let’s see a computed property in action using a `Rectangle` class:

class Rectangle {
var width: Double
var height: Double

init(width: Double, height: Double) {
self.width = width
self.height = height
}

var area: Double {
return width * height
}
}

In this example, `area` is a computed property of the `Rectangle` class. It calculates the area of the rectangle based on the values of the `width` and `height` properties. Computed properties enable us to perform dynamic calculations on the fly, ensuring we always get the most up-to-date result.

Example Question: Implementing Temperature Struct

Let’s put our understanding of stored and computed properties into practice with a real-world example. Imagine we need to work with temperature values in both Celsius and Fahrenheit. We want to create a `Temperature` struct with properties for degrees Celsius and Fahrenheit as `Double`. One of the properties should be stored, and the other should be computed, and they should always be in sync.

Question:

  • Design a Swift `Temperature` struct with properties for degrees Celsius and Fahrenheit. Ensure that changing one property automatically updates the other to keep both values in sync.
struct Temperature {
// Stored property for degrees in Celsius
// Computed property for degrees in Fahrenheit
// Make sure they are always in sync
}

Take a moment to think about your answer before moving on to the next section.

Solution: Implementing the Temperature Struct

To solve the question, we need to create a stored property for degrees in Celsius and a computed property for degrees in Fahrenheit. Let’s implement the `Temperature` struct to keep both properties in sync:

struct Temperature {
// Stored property for degrees in Celsius
var celsius: Double {
didSet {
fahrenheit = (celsius * 9/5) + 32
}
}

// Computed property for degrees in Fahrenheit
private var fahrenheit: Double {
didSet {
celsius = (fahrenheit - 32) * 5/9
}
}

init(celsius: Double) {
self.celsius = celsius
self.fahrenheit = celsius * 9/5 + 32
}
}

In this solution, we have a stored property `celsius` for degrees in Celsius and a private computed property `fahrenheit` for degrees in Fahrenheit. We use the `didSet` property observer for both properties to ensure that changing one property automatically updates the other, keeping both values in sync. Additionally, we initialize `fahrenheit` in the `init` method based on the initial value of `celsius`.

Conclusion

In this blog post, we explored the fundamental concepts of stored and computed properties in Swift. Stored properties are like regular variables, while computed properties provide dynamic calculations based on other properties or custom logic. We also put our knowledge into practice with a real-world example by designing the `Temperature` struct.

Understanding the differences between stored and computed properties allows you to build more efficient and maintainable code for your iOS app development projects. Remember, Swift offers many other powerful property features, such as property observers, lazy properties, and static properties, which you can explore in your further iOS development journey. Happy coding!

— -

--

--