Properties associate values with a particular class, structure, or enumeration.
Swift Properties are broadly classified into two types.
Stored Properties: store constants and variables and are provided by classes and structures. Stored properties can be either variable stored properties (introduced by the var keyword) or constant stored properties (introduced by the let keyword).
Computed Properties: Instead of storing values, these calculated values. Are provided by class structs as well as enumerations.
Note: A computed property can’t be a constant.
Example Of Swift Stored Property
1
2
3
4
5
6
7
8
9
structStudent{
let name:String
varid:Int
}
varobj=Rect(name:"John",id:5)
obj.id=5
Both the name and id are stored properties (constant and variable respectively) in the above snippet.
Swift Lazy Properties
A lazy stored property is a property whose initial value is not calculated until the first time it is used. You indicate a lazy stored property by writing the lazy modifier before its declaration.
NOTE: You must always declare a lazy property as a variable (with the var keyword), because its initial value might not be retrieved until after instance initialization completes. Constant properties must always have a value before initialization completes, and therefore cannot be declared as lazy.
Example Of Swift Lazy Property
1
2
3
4
5
6
7
8
classTestClass{
lazy vartestString:String="Test"
}
let testClass=TestClass()
print(testClass.testString)//TestString
The property will not initalized until it gets accessed. In this example this is not very obivious though. But since the initialization can also happen inside a block, we can make it a little bit more obvious:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classTestClass{
lazy vartestString:String={
print("initialize the property")
return"Test"
}()
}
let testClass=TestClass()
print("before first call")
print(testClass.testString)
Output is:
before first call
initialize the property
TestString
Swift Computed Properties
In addition to stored properties, 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
structRect{
varlength:Double
let breadth:Double
vararea:Double{
get{
returnlength*breadth
}
set(newArea)
{
length=newArea/breadth
}
}
}
varr=Rect(length:6,breadth:5)
print(r.area)//prints 30.0
r.area=40
print(r.length)//prints 8.0
In the above code getters and setters are used as get { } and set(param_name){ } on the computed property area. The getters and setters are accessed using the dot syntax. If a param name isn’t specified inside the setter, Swift assigns the default name as newValue.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
structRect{
varlength:Double
let breadth:Double
vararea:Double{
get{
returnlength*breadth
}
set
{
length=newValue/breadth
}
}
}
Computed Properties can’t be assigned as a lazy var property. Computed Properties that have a get and set defined can’t be set as a constant let.
Swift Property Observers
Swift Property Observers respond to changes in the property value. These are typically used when two property values are dependent on each other. They contain two methods:
willSet : This gets triggered just before the value is stored. It allows us to read the old value before its changed. We can access the new value using the keyword newValue
didSet : This gets triggered after the value is stored. It lets us read both the old and new values. We can access the old value using the keyword oldValue
Property Observers get triggered every time the value is set.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
importUIKit
structPerson{
varname:String
varage:Int
}
classViewController:UIViewController{
varperson:Person?{
didSet{
print("Called after setting the new value")
iflet name=person?.name{
print("New name is \(name) and old name is \(String(describing:oldValue?.name))")
}
}
willSet(myNewValue){
print("Called before setting the new value")
iflet newName=myNewValue?.name{
print("New name is \(newName)")
}
}
}
override func viewDidLoad(){
super.viewDidLoad()
person=Person(name:"webkul",age:4)
}
}
Output:-
Called before setting the newvalue
Newname iswebkul
Called after setting the newvalue
Newname iswebkul andold name isnil
Conclusion
So pls follow the above step and And if you have any issue or suggestion you can leave your message in the comment section I will try to solve this.
Specialist in Flutter, Swift, and Dart with a focus on iOS development. Creates cutting-edge apps that are responsive, high-performing, and aligned with client needs.
Be the first to comment.