Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift function vs lazy var vs computed property - difference?

Tags:

swift

Is there a difference between following:

Lazy variable:

lazy var profileImageIsLoaded : Bool = {     return (profileImageView.image != nil) && (profileImageProgressView.alpha == 0.0) }() 

Function:

func profileImageIsLoaded() -> Bool {     return (profileImageView.image != nil) && (profileImageProgressView.alpha == 0.0) } 

Computed Property:

var profileImageIsLoaded : Bool {     return (profileImageView.image != nil) && (profileImageProgressView.alpha == 0.0) } 

and what method would be the best to use?

I would call the function/variable multiple times, so my question is also whether lazy vars are "updated" or whether they only get a value one time.

like image 973
j3141592653589793238 Avatar asked Jan 01 '18 13:01

j3141592653589793238


People also ask

What is lazy var Swift?

A lazy var is a property whose initial value is not calculated until the first time it's called. It's part of a family of properties in which we have constant properties, computed properties, and mutable properties.

What is a computed property in Swift?

Computed properties are part of a family of property types in Swift. Stored properties are the most common which save and return a stored value whereas computed ones are a bit different. A computed property, it's all in the name, computes its property upon request.

What are stored and computed properties in Swift?

There are two kinds of properties: stored properties and computed properties. Stored properties are properties that are stored in the class's instance. Stored properties store constant and variable values. Computed properties are for creating custom get and set methods for stored properties.

What is willSet and didSet in Swift?

willSet is called before the data is actually changed and it has a default constant newValue which shows the value that is going to be set. didSet is called right after the data is stored and it has a default constant oldValue which shows the previous value that is overwritten.


2 Answers

  • lazy vars are actually stored properties, so you can't put it in extensions or anywhere stored properties are not allowed.
  • The getter for computed properties is run every time you refer to that property. This can be significant especially if the getter is time-consuming or has side-effects to other parts of the code.
  • The getter for lazy vars are only run when the property is first referred to and never again.
  • lazy vars are variables. You can mutate them.
  • Computed properties can optionally have a setter, so sometimes they are read-only.
  • Using a function like that is very similar to a read only computed property. You just have to add () when getting its value.
like image 121
Sweeper Avatar answered Oct 11 '22 12:10

Sweeper


The first one:

lazy var profileImageIsLoaded: Bool = {     return (profileImageView.image != nil) && (profileImageProgressView.alpha == 0.0) }() 

profileImageIsLoaded is a stored property that is initialized lazily using a closure, once the variable has been initialized this closure will not be called anymore and the value it took the first time the closure was called will be returned.

The second one:

func profileImageIsLoaded() -> Bool {     return (profileImageView.image != nil) && (profileImageProgressView.alpha == 0.0) } 

Is a normal function declaration, this is declaration only. If you wanted to call that function you'll do that like this: profileImageIsLoaded().

The third one:

var profileImageIsLoaded: Bool {     return (profileImageView.image != nil) && (profileImageProgressView.alpha == 0.0) } 

profileImageIsLoaded is a computed property, each time you access this property, it will be computed and returned.

Choosing which one to use always depends on your situation.

like image 26
Mo Abdul-Hameed Avatar answered Oct 11 '22 13:10

Mo Abdul-Hameed