You are on page 1of 2

🤔 🐧 🔥 🐶 😋 🐙

V"ri"ble Observers

1. Wh"t is " V"ri"ble Observer

Noice! A v"ri"ble observer is custom code used only for setting
v"ri"bles.Specific6lly, right before (willSet) 6nd right 6fter (didSet).

2. Quick ch"t "bout Computed Properties

You might be 6sking how 6 v6ri6ble observer is different th6n 6 computed
property! Simply put, 6 computed property of 6 v6ri6ble is
the getter 6nd setter methods which c6n be modified. While v"ri"ble
observers just look 6t the time right before 6nd "fter setting " new v"lue.

3. Why they m"tter
V6ri6ble Observers 6re gre6t they 6re useful for v"lue ch"nge
They 6lso c6n be used to keep rel"ted v"lues in sync. Keep your view
upd6ted with wh6tever d6t6 you get

4. How to implement
It is very e6sy to implement! Within the decl6r6tion of your v6ri6ble you c6n
6dd custom code th6t will be executed prior 6nd right "fter the v6lue of the
v6ri6ble is ch6nged.

v6r testString: String = "Hello World" {

After setting
didSet {
// Code you w6nt to execute 6fter 6 new v6lue is set
Prior to setting
willSet {
// Code you w6nt to execute right before setting 6 new v6lue

5. Speci"l notes
One sm6ll observ6tion th6t you m6y h6ve looked over  : property observers
6re only useful for v"ri"bles (v6r properties), 6nd c"nnot be written
for const"nts (let properties). This is bec6use let properties will never ch6nge
v6lue once initi6lized 6nd therefore will never invoke willSet or didSet 
Property observers 6re not c"lled during initi"liz"tion