Swift Properties: Stored, Lazy, and Computed
In Swift, properties are used to store values associated with a particular instance of a class, structure, or enumeration. There are three types of properties in Swift: stored properties, lazy 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, structure, or enumeration. They are declared as constants or variables and must be assigned a default value at initialization.
Here is an example of a stored property in Swift:
1struct Rectangle { 2 var width: Double 3 var height: Double 4 var area: Double 5} 6 7var rect = Rectangle(width: 5, height: 10, area: 0) 8rect.area = rect.width * rect.height 9print("The area of the rectangle is \(rect.area)")
In the above example, width
and height
are stored properties of the Rectangle
struct, and area
is a computed property that is calculated based on the values of width
and height
.
Lazy Stored Properties
Lazy stored properties are used to delay the initialization of a property until its first use. They are declared with the lazy
keyword and are only calculated once. Lazy stored properties must also be declared as variables.
Here is an example of a lazy stored property in Swift:
1class DataImporter { 2 var filename = "data.txt" 3 //... 4} 5 6class DataManager { 7 lazy var importer = DataImporter() 8 var data = [String]() 9 //... 10} 11 12let manager = DataManager() 13manager.data.append("Some data") 14print(manager.importer.filename)
In the above example, the importer
property of the DataManager
class is a lazy stored property. It is not initialized until it is first used, which happens when we access the filename
property of the DataImporter
instance.
Computed Properties
Computed properties are used to calculate a value based on other properties or values. They do not store a value themselves, but instead provide a getter and/or a setter to retrieve and set the value. Computed properties are declared using the var
keyword and are always variable.
Here is an example of a computed property in Swift:
1class Circle { 2 var radius: Double 3 4 var area: Double { 5 get { 6 return Double.pi * radius * radius 7 } 8 set(newArea) { 9 radius = sqrt(newArea / Double.pi) 10 } 11 } 12 13 init(radius: Double) { 14 self.radius = radius 15 } 16} 17 18var circle = Circle(radius: 5) 19print("The area of the circle is \(circle.area)") 20circle.area = 100 21print("The new radius of the circle is \(circle.radius)")
In the above example, area
is a computed property of the Circle
class that calculates the area of the circle based on its radius
. The get
block is used to calculate and return the area, while the set
block is used to calculate the new radius based on the area.
Comparison Table
Here is a comparison table that summarizes the differences between stored, lazy stored, and computed properties:
Property Type | Definition | Accessible via | Set Value? | Initial Value? | Can be Computed? |
---|---|---|---|---|---|
Stored Properties | A property that stores a constant or variable value as part of an instance. | Instance name | Yes | Yes | No |
Lazy Stored Property | A property that delays initialization until the first time it is accessed. | Instance name | Yes | Yes | Yes |
Computed Properties | A property that does not store a value directly but instead provides a getter and an optional setter to retrieve and set other properties and values indirectly. | Property name (get/set) | Yes | No | Yes |