Properties: Stored, Computed & Observers

Properties are how types expose their data. By the end of this lesson you'll tell stored from computed properties, write get / set , react to changes with willSet / didSet , and use lazy and static .

Learn Properties: Stored, Computed & Observers in our free Swift course — a beginner-friendly interactive lesson with worked examples, a practice exercise…

Part of the free Swift course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

What You'll Learn in This Lesson

1️⃣ Stored Properties

A stored property holds a real value as part of the instance. It can be a var (changeable) or let (fixed), and may have a default value.

2️⃣ Computed Properties (get / set)

A computed property stores nothing — it calculates its value each time via a get ter. Add a set ter to make it read/write; the assigned value arrives as newValue .

3️⃣ Property Observers (willSet / didSet)

Observers run code around a change to a stored property. willSet runs just before (with newValue ); didSet runs just after (with oldValue ).

4️⃣ lazy & static

lazy defers a stored property's initialization until first access (must be a var ). static attaches a property to the type itself — one shared copy across all instances.

Your turn. Fill in the blanks, then run it and check the output.

📋 Quick Reference

Build it, run it, and check your output against the example in the comments.

Practice quiz

What is a stored property?

  • A value computed each time
  • A method
  • A constant or variable that stores a value as part of an instance
  • A protocol

Answer: A constant or variable that stores a value as part of an instance. Stored properties hold an actual value as part of the instance.

What is a computed property?

  • A property that calculates its value each time it's accessed
  • A property that stores a value
  • A static constant
  • A method with no name

Answer: A property that calculates its value each time it's accessed. Computed properties run code to derive a value rather than store one.

Which clause is required for a read-only computed property?

  • set only
  • didSet
  • willSet
  • get (the getter)

Answer: get (the getter). A computed property needs at least a getter to return its value.

When does a observer run?

  • Before the value changes
  • Just after the property's value is set
  • Only at init
  • Never

Answer: Just after the property's value is set. didSet runs immediately after the new value has been stored.

When does a observer run?

  • Just before the property is set to a new value
  • After the value changes
  • Only on read
  • At deinit

Answer: Just before the property is set to a new value. willSet runs right before the new value is assigned.

Inside willSet, what is the default name for the incoming value?

  • oldValue
  • value
  • newValue
  • next

Answer: newValue. willSet provides newValue by default; didSet provides oldValue.

What does a stored property do?

  • Loads at app launch
  • Is only computed/initialized on first access
  • Cannot be changed
  • Runs on a background thread

Answer: Is only computed/initialized on first access. A lazy property defers its initialization until first accessed.

Can a lazy property be a constant declared with ?

  • Yes
  • Only in classes
  • Only with get/set
  • No — lazy must be a var

Answer: No — lazy must be a var. lazy properties must be var because their value is set after init, on first use.

What does the keyword create on a type?

  • An instance property
  • A type-level property shared across all instances
  • A computed getter
  • A deinitializer

Answer: A type-level property shared across all instances. static makes a property (or method) belong to the type itself, not instances.

Do computed properties store a value in memory?

  • Yes, like stored properties
  • Only the first time
  • No — they compute on each access
  • Only if lazy

Answer: No — they compute on each access. Computed properties hold no storage; they recalculate each time.