SwiftJuniorMCQ

What is a read-only computed property?

Test your knowledge:

Explanation & Code

Answer: A read-only computed property has a getter but no setter, so it can be read but never assigned to.

Code Example:

struct Cuboid {
    var width = 0.0, height = 0.0, depth = 0.0

    // Full form
    var volume: Double {
        get { width * height * depth }
    }

    // Shorthand — drop the `get` keyword and braces
    var surfaceArea: Double {
        2 * (width * height + width * depth + height * depth)
    }
}

let box = Cuboid(width: 2, height: 3, depth: 4)
print(box.volume) // 24.0
// box.volume = 10 // ❌ Cannot assign to property: 'volume' is a get-only property

Key Points:

  • Must still be declared with var, not let — its value can change between reads because the inputs can change.
  • let means "this value is fixed for the instance's lifetime"; a computed property makes no such promise.
  • Read-only is different from private(set) var, which is a stored property with a restricted setter.

Rate your understanding:

Ready to practice more Swift?

Test yourself with our interactive quiz mode or browse all curated questions for this topic.