Property Observers

Swift provides an interesting feature called property observation. Property observers watch for and respond to changes in a given property. Property observation is available to any stored property that you define and is also available to any property that you inherit. You cannot use property observers with computed properties that you define. (But you have full control over the definition of a computed property’s setter and getter, and can respond to changes there.)

Imagine that the citizens of your beleaguered town are getting restless. They demand that the mayor do something to protect them from the monstrous pox patrolling the countryside. The mayor’s first action is to track the attacks on the townspeople. Property ...

Get Swift Programming: The Big Nerd Ranch Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.