ProgramGuru

Stored properties, computed properties and observers

Stored properties, computed properties and observers

A report has a stored score and a pass status derived from that score. Storing both independently invites disagreement after a correction. A computed property calculates its value when accessed, while a stored property retains a value between operations.

What you’ll learn

  • Derive a pass decision with a computed property.
  • Explain why stored, computed and lazy properties differ.

Stored properties, computed properties and observers in the example

  1. Stored score

    A report has a stored score and a pass status derived from that score.

    Stored score
  2. Computed pass decision

    A computed property’s getter can read other state and return a derived value.

    Computed pass decision
  3. Method returns summary

    At score 70, the method reads the computed passed property and returns Practice.

    Method returns summary
At score 70, the method reads the computed passed property and returns Practice. After score changes to 80, the next access recalculates passed as true. No separate pass flag needs updating.

How stored properties, computed properties and observers works

A computed property’s getter can read other state and return a derived value. A setter can define how a write updates underlying state. Property observers willSet and didSet react to writes under their language rules; they do not make a separate stored field permanently consistent by themselves.

Work through stored properties, computed properties and observers

Save this complete program as main.swift and run it with the Swift toolchain. No separate input file is needed.

Swift · complete program

struct Report {
  var score: Int
  var passed: Bool { score >= 75 }
  func summary() -> String { passed ? "Pass" : "Practice" }
}
var report = Report(score: 70)
print(report.summary())
report.score = 80
print(report.passed)

Expected output

Practice
true

Follow the execution

At score 70, the method reads the computed passed property and returns Practice. After score changes to 80, the next access recalculates passed as true. No separate pass flag needs updating.

Try this program in the Swift compiler. Paste the complete source above.

Practice: Stored properties, computed properties and observers

Your task: Why is passed computed instead of independently stored here?

Swift · complete program

struct Report {
  var score: Int
  var passed: Bool { score >= 75 }
  func summary() -> String { passed ? "Pass" : "Practice" }
}
var report = Report(score: 70)
print(report.summary())
report.score = 80
print(report.passed)

Worked answer

It is determined entirely by the score and stated threshold. Computing it keeps that relationship consistent whenever the score changes.

Derive values instead of duplicating state

A computed getter can perform work and is not automatically cached. Avoid expensive or surprising side effects in an ordinary-looking property read. A lazy stored property delays its initial creation but then stores a value; that is different from recomputing on every access.

Check your decision

Why is passed computed instead of independently stored here?
Choose one answer. You can try again.

Answer and explanation

It is determined entirely by the score and stated threshold.

It is determined entirely by the score and stated threshold. Computing it keeps that relationship consistent whenever the score changes.

Derive values instead of duplicating state

It is determined entirely by the score and stated threshold. Computing it keeps that relationship consistent whenever the score changes. A computed getter can perform work and is not automatically cached.

Further reading: official Swift language guide.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Initialization and valid starting state →