O'Reilly logo

Mastering Swift 4 - Fourth Edition by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Property observers

Property observers are called every time the value of the property is set. We can add property observers to any non-lazy stored property. We can also add property observers to any inherited stored or computed property by overriding the property in the subclass. We will look at the Overriding properties section a little later in this chapter.

There are two property observers that we can set in Swift: willSet and didSet. The willSet observer is called right before the property is set, and the didSet observer is called right after the property is set.

One thing to note about property observers is that they are not called when the value is set during initialization. Let's look at how we can add a property observer to the salary ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required