SLN SLN - 2 years ago 61
Swift Question

Computed properties cannot store a value but hold a value, what is hold and how to hold?

Quote from "Swift Apprentice second edition page 225":

Properties(computed) hold values that you can get and set. Methods perform work

Quote from "Swift official document":

In addition to stored properties, classes, structures, and enumerations can define computed properties, which do not actually store a value

One says "store", another one says "hold". I get a bit confused about the computed properties, what exactly the computed properties do not store a value means? Is it means the system will not assign any memory space to save the value and will only hold it. But what's the hold means? you can't hold something in nowhere, you need maybe a box to hold something, isn't it?

Answer Source

Simple example from the documentation

struct Cuboid {
    var width = 0.0, height = 0.0, depth = 0.0
    var volume: Double {
        return width * height * depth

volume is never stored in memory. Whenever the getter is called the result of the multiplication is returned dynamically like a math operation in a local scope.

The official Apple documentation does not use the term hold at all.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download