Property Observers in Swift

Seeing changes

Image for post
Image for post

Images by Chewy Pets Bringing us Together

Before we start

Prerequisites

Keywords and Terminology

This article

Background

This article on the other hand covers Property Observers, and it is about time that I did just that.

The Repo

What is a Property Observer anyway?

The use of Property Observers

This would involve (perhaps) the use of a timer and perhaps a closure, and this is not a good approach for a well-coded App.

Much better would to be observe when changes are made, and then react accordingly. In steps Property Observers.

An example

This is then kicked off with an instance of the Game class, and playerOne won the first game! This means that we will set the score to be 1, and then print out the score. We will do that by using the following three lines:

The answer

What if the property is changed to the same value?

Let us write out this line:

So we have three lines that are then written to the console

Conclusion

The Repo makes things rather easier to follow in this project, and I do recommend you download this project.

If you’ve any questions, comments or suggestions please hit me up on Twitter

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store