In: Computer Science
In swift language, what is the difference between a stored property, computed property, property observers and lazy property?
Usually properties used to store the values. In swift language there are two kinds of property available to store the values.
Stored Property:
A constant or variable is stored as part of the instance. It used variable stored property, constant stored property. In variable stored property achieved by the word var and constant stored property achieved by the word let.
example:
struct Range{
var StartingRange : Int
let length : Int
var range=Range(StartingRange:0,length:3)
//range value starts from 0,1,2
range.StartingRange = 6
//the range starts from 6,7,8
}
In the above example we have used the variable stored property for the StartingRange variable. Hence we can changed in the middle of the program. But we have used constant stored property for the length variable. So that we cannot change the length 3.
Computed Property:
It provide a getter and setter method to set and retrieve the other property and values indirectly.
Example:
class Rectangle{
var length: Double?
var height : Double?
var Area: Double {
get {
return length! * height!
}
set (newArea){
cval = Double(newArea)
}
}
}
let result = Rectangle()
result.Area = 20
print(result.cval)
In the above example it use the get and set method for calculating the area of a Rectangle.
Property Observers:
Property observers observe and respond to changes in a property's value. Property observers are called every time a property's value is set, even if the new value is the same as the property's current value. Property observers is really used when we want to configure a view based on a given value.
Lazy Property:
In Lazy property, when the initial value for a property is dependent on outside factors whose values are not known until after an instance’s initialization is complete. Lazy properties are also useful when the initial value for a property requires complex or computationally expensive setup that should not be performed unless or until it is needed.
Stored Property | Computed Property | Lazy Property | Property Observers |
A stored property is a constant or variable that is stored as part of an instance of a particular class or structure | In Computed Property classes, structures, and enumerations can define computed properties, which do not actually store a value. Instead, they provide a getter and an optional setter to retrieve and set other properties and values indirectly | In Lazy property, when the initial value for a property is dependent on outside factors whose values are not known until after an instance’s initialization is complete. | Property observers observe and respond to changes in a property's value. |
Whether the variable used or not but the space was allocated for the stored property. | In Computed property has both getter and setter to store and retrieve the values | In Lazy property closure associated with the lazy property is executed only if you used that property. If for some reason that property is not used you avoid unnecessary allocation and computation. | Property observers are called every time a property's value is set, even if the new value is the same as the property's current value. |