What are computed properties vs stored properties?

In Swift, properties are variables that are associated with a class, structure, or enumeration. There are two main types of properties: stored properties and computed properties.

Stored Properties

Stored properties are used to store constant or variable values as part of an instance of a class or structure. They are defined using the `var` or `let` keywords and hold data directly.

Computed Properties

Computed properties do not store a value; instead, they provide a getter and an optional setter to retrieve and set other properties or values indirectly. They are defined using the `var` keyword and include a getter and, if necessary, a setter.

Example:

class Rectangle { var width: Double var height: Double // Stored properties init(width: Double, height: Double) { self.width = width self.height = height } // Computed property var area: Double { return width * height } } let rectangle = Rectangle(width: 10, height: 5) print("Area of the rectangle: \(rectangle.area)") // Output: Area of the rectangle: 50.0

Swift Stored Properties Computed Properties Variables Class Structure Getter Setter