computed property vs stored property (swift)

http://stackoverflow.com/questions/31515805/difference-between-computed-property-and-property-set-with-closure

Swift: Beware of Computed Properties

The computed property’s block is called each time you reference the variable

Computed Property:

Stored Property

ASSUMING THAT YOU DO NOT set the stored property, the stored property’s initialization closure is called once and only once when you do the first READ. Any other READS after that will simply return the variable itself. Thus, it is useful for setting default values.

In other words, (granted you do not set it) it is calculated only when the variable is accessed for the first time. Then, it simply just returns the variable.

However, if you were to set the stored property, the initial closure will not be run, and it will simply access the variable
for you to use:

Now let’s test it. As you can see, if you READ the property name for the first time, the lazy load will be called. Every READ after that will be a direct access and it won’t call the function anymore.

output:

—-init Test class—–
— load data for stored property ‘name’ —
Ricky
Ricky
Hannah

Simply using SET will not call the closure

When the property ‘name’ is set explicitly, it doesn’t do the lazy loading.