Sometimes you want to execute some code in Swift 3 whenever a variable changes its value. And perhaps you’ve done this previously by creating a function that you call whenever it makes sense in your code.

However, there’s a MUCH better way!

We’ll learn how to use observers which act like watchdogs – they bark whenever even the tiniest part of a variable changes!

Declare a variable and implement the didSet function:


There are also other functions you can override in there, such as willSet, called BEFORE the variable is actually changed:


You can even stack these:

Of course there are many more functions you can override so I dare you to go look for them 😉


Where to go Next

This tutorial comes straight out of The Complete iOS Developer Course. Over 40 hours long it contains everything you need to create awe inspiring apps fast!

Click here to check it out!

How to Observe Changes in Variables with Swift 3 and iOS 10

Leave a Reply

Your email address will not be published. Required fields are marked *

Share This

Want this Post in Easy to Understand Video Form?

Then get the Complete iOS Developer Course. Over 50 hours of pure Swift coding goodness designed to turn you into a coding master!

One Time Offer - $9! Click to Get it!

WARNING: Once this box is closed the offer will not be shown again.

Thank you!