Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between computed property and property set with closure

I'm new to Swift. What is the difference between a computed property and a property set to a closure? I know a computed property gets recalculated each time. Is it different for the closure? i.e.

Closure:

var pushBehavior: UIPushBehavior = {     let lazilyCreatedPush = UIPushBehavior()     lazilyCreatedPush.setAngle(50, magnitude: 50)     return lazilyCreatedPush }() 

Computed:

var pushBehavior: UIPushBehavior {     get{         let lazilyCreatedPush = UIPushBehavior()         lazilyCreatedPush.setAngle(50, magnitude: 50)         return lazilyCreatedPush     } } 
like image 512
bakalolo Avatar asked Jul 20 '15 11:07

bakalolo


People also ask

What is the meaning of computed property?

A Computed Property provides a getter and an optional setter to indirectly access other properties and values. It can be used in several ways. A common use-case is to derive value from other properties.

What is computed property in Swift?

Swift Computed Property Here, num1 is a stored property, which stores some value for an instance of Calculator . However, there is another type of property called computed properties. Unlike stored property, a computed property calculates the value. For example, class Calculator { ...

What is stored properties in Swift?

Stored Properties A stored property is a property whose value is stored as part of the instance of a particular type. Stored properties can be either variable or constant. We can use var to create a variable stored property, and let to create a constant stored property.


2 Answers

In short, the first is a stored property that is initialized via a closure, with that closure being called only one time, when it is initialized. The second is a computed property whose get block is called every time you reference that property.


The stored property’s initialization closure is called once and only once, but you can later change the value of the stored property (unless you replace var with let). This is useful when you want to encapsulate the code to initialize a stored property in a single, concise block of code.

The computed property’s block, however, is called each time you reference the variable. It’s useful when you want the code to be called every time you reference the computed property. Generally you do this when the computed property needs to be recalculated every time you reference the stored property (e.g. recalculated from other, possibly private, stored properties).

In this case, you undoubtedly want the stored property (the first example), not the computed property (the second example). You presumably don't want a new push behavior object each time you reference the variable.


By the way, in your first example, you internally reference to it being instantiated lazily. If you want that behavior, you must use the lazy keyword:

lazy var pushBehavior: UIPushBehavior = {     let behavior = UIPushBehavior()     behavior.setAngle(50, magnitude: 50)     return behavior }() 

If, however, the property is static, it is automatically instantiated lazily.

like image 198
Rob Avatar answered Oct 01 '22 01:10

Rob


Closure :

  //closure     var pushBehavior: UIPushBehavior = {         let lazilyCreatedPush = UIPushBehavior()         lazilyCreatedPush.setAngle(50, magnitude: 50)         return lazilyCreatedPush     }() 

At first time when pushBehavior variable called then block execute and value is saved in pushBehavior variable. after that whenever you call pushBehavior then those value are returned.

means only first-time block code executed and saved in this variable. Also, you can store variable value whenever you want but after that, those value returned but if you declare as "let" then you can't change this value.

Computed property :

var pushBehavior: UIPushBehavior {     get{         let lazilyCreatedPush = UIPushBehavior()         lazilyCreatedPush.setAngle(50, magnitude: 50)         return lazilyCreatedPush     } } 

In computed property whenever you called pushBehavior variable then this block execute and value return. so every time block is executed. and you can not declare variable as "let" keyword for pushBehavior variable.

So you can use this code as per your requirement.

like image 23
vikas prajapati Avatar answered Oct 01 '22 00:10

vikas prajapati