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 


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.
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"

getters and setters in Swift?How do I create a getter and setter in Swift?What is a type property?

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.


computed property in swift,computed property in swiftui,computed property in swift 4/Swift 5,computed property in extension swift,computed property in enum swift,computed property in struct swift,swift property observer,'didset' cannot be provided together with a getter,swift static property,getter/setter swift,swift didset change value,swift didset not called,static computed property swift,computed property in protocol swift,swift readonly property,swift static property,swiftproperty observer.stored properties in swift extensions,stored property swift closure.lazy stored properties in swift example


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".

What is didSet and willSet in Swift? What are property observers in Swift?  What are getters and setters in Swift? How do I create a getter and setter in Swift? What is a type property?

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.
stored property vs computed property in swift,stored property in enum swift,property observer swift class,swift didset not called computed property in swift,computed property in swiftui,computed property in swift 4/Swift 5,computed property in extension swift,computed property in enum swift,computed property in struct swift,swift property observer,'didset' cannot be provided together with a getter,swift static property,getter/setter swift,swift didset change value,swift didset not called,static computed property swift,computed property in protocol swift,swift readonly property,swift static property,swiftproperty observer.stored properties in swift extensions,stored property swift closure.lazy stored properties in swift example

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).


stored property vs computed property in swift,stored property in enum swift,property observer swift class,swift didset not called computed property in swift,computed property in swiftui,computed property in swift 4/Swift 5,computed property in extension swift,computed property in enum swift,computed property in struct swift,swift property observer,'didset' cannot be provided together with a getter,swift static property,getter/setter swift,swift didset change value,swift didset not called,static computed property swift,computed property in protocol swift,swift readonly property,swift static property,swiftproperty observer.stored properties in swift extensions,stored property swift closure.lazy stored properties in swift exampleWhat is didSet and willSet in Swift? What are property observers in Swift?  What are getters and setters in Swift? How do I create a getter and setter in Swift? What is a type property?   swift computed property get set stored and computed properties in swift didset and willset in swift computed property swift example swift getter/setter swift computed property with parameter

Computed properties with Struct: 

We can see Same example with struct 

What is didSet and willSet in Swift? What are property observers in Swift?  What are getters and setters in Swift? How do I create a getter and setter in Swift? What is a type property?   swift computed property get set stored and computed properties in swift didset and willset in swift computed property swift example swift getter/setter swift computed property with parameter  stored property vs computed property in swift,stored property in enum swift,property observer swift class,swift didset not called computed property in swift,computed property in swiftui,computed property in swift 4/Swift 5,computed property in extension swift,computed property in enum swift,computed property in struct swift,swift property observer,'didset' cannot be provided together with a getter,swift static property,getter/setter swift,swift didset change value,swift didset not called,static computed property swift,computed property in protocol swift,swift readonly property,swift static property,swiftproperty observer.stored properties in swift extensions,stored property swift closure.lazy stored properties in swift example

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.

 stored property vs computed property in swift,stored property in enum swift,property observer swift class,swift didset not called computed property in swift,computed property in swiftui,computed property in swift 4/Swift 5,computed property in extension swift,computed property in enum swift,computed property in struct swift,swift property observer,'didset' cannot be provided together with a getter,swift static property,getter/setter swift,swift didset change value,swift didset not called,static computed property swift,computed property in protocol swift,swift readonly property,swift static property,swiftproperty observer.stored properties in swift extensions,stored property swift closure.lazy stored properties in swift example

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:


stored property vs computed property in swift,stored property in enum swift,property observer swift class,swift didset not called computed property in swift,computed property in swiftui,computed property in swift 4/Swift 5,computed property in extension swift,computed property in enum swift,computed property in struct swift,swift property observer,'didset' cannot be provided together with a getter,swift static property,getter/setter swift,swift didset change value,swift didset not called,static computed property swift,computed property in protocol swift,swift readonly property,swift static property,swiftproperty observer.stored properties in swift extensions,stored property swift closure.lazy stored properties in swift 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!!

---------------------------





Post a Comment

0 Comments