Getters and Setters in swift | How to work willSet and didiSet? | Stored property | Computed property | Property Observers

Stored property | Computed property | Property Observers | Swift 5 (Xcode 11)


Hello Friends, I am trying to give some idea about properties in Swift.how to work willSet and didSet? and getter/setter work in Swift iOS.

Swift properties which are basically are associated values with a particular class, struct & enum.
Properties have two types:
  1. Stored property
  2. 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 a 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.👍
Friends,You can see this article in my youtube channel.👏
Click here to subscribe now.





Post a Comment

0 Comments