You must be familiar with the observer pattern in object-c. Swift provides a simpler way to implement
When we need to assign values to the object's property values, we can use Willset and didset to observe the changes in the object's property values.
Let's look directly at an example. Create a class Student to monitor the property name
The first is the definition of the student class.
Class student{ init (name:string) { username=name; } var username:string= "" { willset{ println ("Student name Newvalue:\ (newvalue)") } didset{ println ("Student name Oldvalue:\ (oldValue)")}}
And then we initialize the student and modify his username to try the effect.
var student=student (name: "Zhang San")//Note that the time of initialization is student.username= "Lisi" without invoking the monitoring method
Printing results are as follows
Student name Newvalue:lisi
Student name OldValue: Zhang San
It's easy, there's a problem to keep talking about.
Apple Development Group: 414319235 Welcome to join the Welcome discussion question
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Swift Property Value Monitoring