Quickâwhat kind of property is this?
a.femurToHeightIf you thought:
âThatâs just reading a property from an instance of a structure.â
âŚyouâre absolutely right.
And thatâs the point: reading a computed property looks exactly the same as reading a stored property.
The difference is where the value comes from.
- Stored property: a value saved inside the instance when it was created.
- Computed property: a value calculated on demand from stored values each time you access it.
Stored properties
In a new Xcode Playground named ComputedProperties please copy this code and run it:
// Define the structure
struct Person {
// MARK: Stored properties
let heightCm: Double
let armSpanCm: Double
let femurCm: Double
let headLengthCm: Double
}
// Sample instances (use realistic numbers or your own)
let a = Person(heightCm: 170, armSpanCm: 171, femurCm: 43, headLengthCm: 21)
let b = Person(heightCm: 162, armSpanCm: 160, femurCm: 40, headLengthCm: 19)
let c = Person(heightCm: 180, armSpanCm: 182, femurCm: 46, headLengthCm: 22)
// Query STORED properties (each line shows a result in the sidebar):
a.heightCm
a.armSpanCm
a.femurCm
a.headLengthCm
b.heightCm
b.armSpanCm
b.femurCm
b.headLengthCm
c.heightCm
c.armSpanCm
c.femurCm
c.headLengthCmWhat to notice: each line simply reads a value that was stored inside a, b, or c when the instance was created.
Computed propertes
Now we will add computed properties to represent common human body ratios.
Common bodyâratio benchmarks (rough guides):
- arm span : height â 1.00
- femur : height â 0.25
- head length : height â 0.10â0.125 (1/10 to 1/8)
With computed properties Swift calculates these ratios from the stored values every time you access them.
Replace your Person structure with this version below (same stored properties + 3 computed properties).
NOTE
Note the explicit
returnused in each computed property.
// Define the structure
struct Person {
// MARK: Stored properties
let heightCm: Double
let armSpanCm: Double
let femurCm: Double
let headLengthCm: Double
// MARK: Computed properties
var armSpanToHeight: Double {
return armSpanCm / heightCm
}
var femurToHeight: Double {
return femurCm / heightCm
}
var headToHeight: Double {
return headLengthCm / heightCm
}
}
// Re-create the same examples
let a = Person(heightCm: 170, armSpanCm: 171, femurCm: 43, headLengthCm: 21)
let b = Person(heightCm: 162, armSpanCm: 160, femurCm: 40, headLengthCm: 19)
let c = Person(heightCm: 180, armSpanCm: 182, femurCm: 46, headLengthCm: 22)
// Query COMPUTED properties
a.armSpanToHeight
a.femurToHeight
a.headToHeight
b.armSpanToHeight
b.femurToHeight
b.headToHeight
c.armSpanToHeight
c.femurToHeight
c.headToHeightWhat is a computed property?
A computed property looks like any other property when you use it, but it doesnât store a value inside the instance.
Instead, it calculates a value from other stored properties each time you access it.
var armSpanToHeight: Double {
return armSpanCm / heightCm
}- Thereâs no memory in the computer that is used to store
armSpanToHeight. - When the line
a.armSpanToHeightruns, Swift executes the code inside{ ... }, does the math, and produces a fresh value right then. - If the stored values changed (in a mutable instance), youâd get an updated result next time you read the computed property.
Key idea: Access looks the same (a.heightCm vs a.armSpanToHeight), but computed properties are derived on demand from the stored data.
What does return do here?
The body of a computed property contains instructions for producing its value.
The return keyword marks the result to hand back.
var femurToHeight: Double {
return femurCm / heightCm
}When Swift evaluates b.femurToHeight:
- It jumps into the braces
{ ... }. - It evaluates
femurCm / heightCm. - It returns that number as the propertyâs value.
- The Results sidebar shows the value on the line where you wrote
b.femurToHeight.
Think of return as the answer line: whatever expression follows it is what the outside world receives when the property is accessed.
Exercises
Using real data from the measurements we took earlier:
-
Three classmates
Create three newPersoninstances using real measurements from your class. For each person, query the stored values (heightCm,armSpanCm,femurCm,headLengthCm) and the computed ratios (armSpanToHeight,femurToHeight,headToHeight).
Observe that accessing stored vs. computed looks identicalâjust a property name on a line. -
Closest to 1:1
Compare each personâsarmSpanToHeightvalue. Which one is closest to 1.00? -
Quarterâheight femur
ComparefemurToHeightfor your people. Who is nearest 0.25? Note the value. -
Head proportion band
ForheadToHeight, is each person closer to 0.10 (1/10) or 0.125 (1/8)? Write a short observation for each person.