Properties in Swift
Properties are associated values with particular Class, Structure and Enumeration. It have some values that we access in our application.
Example:
class PropertyCls {
let a = 10
var b = 15
}
var obj = PropertyCls()
print(obj.a) // prints 10
print(obj.b) // prints 15
obj.b = 20
print(obj.b) // prints 20
Properties classified into two types
- Stored Properties
- Computed Properties
Stored Properties vs Computed properties
Stored properties store constant and variable values as part of instance. Computed properties are instead of storing, calculates a value.
Stored properties can be either variable stored property (by var keyword) or constant stored property (by let keyword) of a particular class or structure. It is not used in enumeration.
Whereas computed properties are provided by class or structure or enumeration. It provide a getter and optional setter to get and set the other properties and values indirectly.
Computed property with a getter and no setter is called read only computed properties.
Example:
class Rectangle {
var width: Double = 0.0 // variable stored property
var height: Double = 0.0 // variable stored property
let length: Int = 10 // constant stored property
var area : Double {
return width * height
} // read only computed property
}
class Square {
var a: Double // variable stored property
var area: Double {
get {
return a * a
}
set {
a = sqrt(newValue)
}
} // get & set computed property
}
Note: We can not set computed properties in its own setter method.
Lazy Stored Properties:
Initial value is not calculated until the first time it is used. Since the value stores after initialize the instance, it is always declared as variable(by var keyword). After access first time, it stores value and then it returns a stored value. Indicate lazy stored properties by provide lazy keyword before declaration.
lazy var a: Int
When to use lazy property:
- Values of the property are not known until after instance’s initialization complete. Might be values of the property depends on the outside factor.
- Initial value of the property demands complex or expensive setup.
Note: Since it is one kind of stored property, we can not used in enumeration, used in class and structure.
Property Observers:
Property observers observe and respond to changes in a property’s value. Since computed properties can not set the property by own setter method, the property observers only applies to the stored properties except for lazy stored property.
Property observers are called every time, when property value is set, even if the new value is same as the current value. By using overriding method, we can add property observers to inherited property.
Two type of property observers are there. We have the option to define either or both on a property.
- willSet is called just before the value stored.
- didSet is called immediately, after the new value stored.
If you implement a willSet property observer, it is passed a new property value as a constant parameter. We can name the parameter or use the default parameter name of newValue.
Similarly, if you implement a didSet property observer, it is passed a old property value as a constant parameter. We can name the parameter or use the default parameter name of oldValue.
Example:
class Student {
var totalMarks: Int = 0 {
willSet {
print("New total marks: \(newValue)")
}
didSet {
print("Current total marks: \(totalMarks), Old total marks: \(oldValue)")
}
}
}
var student = Student()
student.totalMarks = 100
// New total marks: 100
// Current total marks: 100, Old total marks: 0
student.totalMarks = 200
// New total marks: 200
// Current total marks: 200, Old total marks: 100
Thanks and Happy coding !!!