0
0
Swiftprogramming~10 mins

Adding computed properties in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Adding computed properties
Define struct/class
Add stored properties
Add computed property with getter
Access computed property
Return computed value based on stored properties
This flow shows how to add a computed property that calculates a value from stored properties when accessed.
Execution Sample
Swift
struct Rectangle {
  var width: Double
  var height: Double
  var area: Double {
    return width * height
  }
}
Defines a Rectangle struct with width and height, and a computed property area that calculates width times height.
Execution Table
StepActionStored PropertiesComputed Property AccessComputed Property Result
1Create Rectangle with width=5, height=10width=5, height=10N/AN/A
2Access area propertywidth=5, height=10Calculate area = width * height50
3Change width to 7width=7, height=10N/AN/A
4Access area property againwidth=7, height=10Calculate area = width * height70
5End of executionwidth=7, height=10N/AN/A
💡 Execution ends after accessing computed property twice and changing stored property once.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
widthundefined577
heightundefined101010
area (computed)undefinedN/AN/A70
Key Moments - 2 Insights
Why does the computed property 'area' change when we change 'width'?
Because 'area' calculates its value each time it is accessed using the current 'width' and 'height' values, as shown in execution_table rows 2 and 4.
Is 'area' stored in memory like 'width' and 'height'?
'area' is not stored; it is computed on demand when accessed, which is why it updates automatically without storing a separate value (see execution_table steps 2 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'area' after step 2?
A15
B7
C50
D10
💡 Hint
Check the 'Computed Property Result' column at step 2 in the execution_table.
At which step does the 'width' change?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Stored Properties' column to see when 'width' changes from 5 to 7.
If we changed 'height' to 20 at step 3 instead of 'width', what would 'area' be at step 4?
A100
B50
C70
D200
💡 Hint
Recall area = width * height; if width=5 and height=20, area = 5*20 = 100.
Concept Snapshot
Adding computed properties in Swift:
- Use var with a code block { get { ... } } or shorthand { ... }
- Computed properties calculate values on access, no storage
- They can use other stored properties to compute results
- Changing stored properties affects computed property output
- Useful for dynamic values like area, fullName, etc.
Full Transcript
This example shows how to add a computed property in Swift. We define a Rectangle struct with stored properties width and height. Then we add a computed property area that returns width times height. When we create a Rectangle with width 5 and height 10, accessing area returns 50. If we change width to 7, accessing area again returns 70. The computed property recalculates its value each time based on current stored properties. This means area is not stored but computed on demand. This helps keep values up to date without extra storage.