O'Reilly logo

Swift Quick Syntax Reference by Matthew Campbell

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

Chapter    35

Property Observers

You can observe the state of a property with the willSet and didSet keywords. You can use these in the property declaration to assign an action that will occur when a property is about to change and when a property value just changed. Listing 35-1 shows how you would add an action that prints a message to the console log whenever the age property value is about to change and after the age value has changed.

Listing 35-1. Property Observers

class Person {    var name: String = "Name"    var age:Int = 0{    willSet{        println("age value is about to change to \(newValue)")    }    didSet{        println("age value just changed to \(self.age)")    }    }    func profile() -> String {        return "I'm \(self.name) ...

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