Browse Questions
  • A read-only computed property has a getter but no setter, so it can be read but never assigned to.
  • A lazy property is only computed the first time it is accessed.
  • These protocols define how types support equality checks, hashing, and ordering.
  • SSL pinning validates that the server's certificate matches a known copy embedded in the app, preventing man-in-the-middle attacks even with valid certificates.

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.