Stored property | Computed property | Property Observers | Swift 5
Hello Friends, I am trying to give some idea about properties in Swift.
How to work willSet/didSet and getter/setter in Swift iOS.
Swift properties which are basically are associated values with a particular class, struct & enum.
- Stored property
- Computed property
Stored properties can be either variable stored properties (introduced by the var keyword) or constant stored properties (introduced by the let keyword).
Stored properties are provided only by classes and structures.
The example below defines a class called StoredClass, which has two property one is var a and other is let b.
Here variable ‘a’ stores integer value 4. We have defined variable ‘a’ using var keyword , that means its value can be changed.
When We have defined variable ‘b’ using let keyword the difference is, its value cant be changed.
objStore.b = 10 // compiler throws error "Cannot assign to property: 'b' is a 'let' constant"
Mutable Model:
As we all know, Classes are reference type whereas Structures and Enumerations are value type in swift.
we can see example a class, we create instance and try to change variable value.you can see everything is working fine.
Immutable Model:
Whereas if we try to do the same in struct which is value type create an instance let(constance) it will show us a compilation error "Cannot assign to property: 'objStore' is a 'let' constant".
Now, we create instance with var keyword.Everything is working fine.
- You can’t define stored property in extension or protocol
- Only classes and structures can provide stored properties. Enums can’t.
It will show us a compilation error "Enums must not contain stored properties"
Computed property:
Computed properties are always variables (never constants)
Classes, structures, and enumerations can have computed properties.
it provide a getter and an optional setter
- The get keyword makes the computed property readable.
- The set keyword makes the computed property writeable.
Computed properties with Class:
We have one example uses a class Rectangle with two stored properties (width & height) and one computed property (area).
Computed properties with Struct:
We can see Same example with struct
Computed properties with Enum:
We have one example uses a enum StoredEnum that uses a switch statement to switch cases which is available in the Enum and return selected case string.
Property Observers:
Property observers can be added to stored properties to observe changes or perform further action.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.
willSet is called just before the value is stored.
didSet is called immediately after the new value is stored.
Let’s understand it by example:
As you see above example, it can be defined by wiillSet and didSet.willSet will be called before setting a new value and didSet will be called after setting a new value.
If you enjoyed this article please share.
Thanks for reading!!
Related Posts:
- How to convert an APNS token to String from Data in swift
- An Introduction to Swift Property Wrappers
- How to remove duplicate elements from an array with Swift 5
- How to store Model Class data in User Defaults in Swift?
- Adding a Custom Font to Your IOS App
- How to big numbers YAxis format values convert to Indian Numbering Format like k(thousand), L(lakh), Cr(crore), Ar(Arab) and etc.on iOS barchart Swift
---------------------------
0 Comments