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.
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.
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.
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.
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.
lazy var
s are actually stored properties, so you can't put it in extensions or anywhere stored properties are not allowed.lazy var
s are only run when the property is first referred to and never again.lazy var
s are variables. You can mutate them.()
when getting its value.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With