willSet 和 didSet 在 Swift 中用于监控存储型属性的变化,被称为属性观察器(Property Observers)。其用法如下:
1 | class Person { |
需要注意的是:
- 属性观察器只可用于存储型属性,不能用在计算型属性;
- 属性观察器不会监控属性初始化过程,即属性首次赋值的时候是不会被监控的,如上面代码的构造函数是不会被监控的,即使属性声明的时候已经初始化;
- 但是不代表构造函数执行一定不会触发属性观察器,下面会举例。
1 | class Person { |
在这个例子中,Student 类的构造函数完成了三件事:
- 初始化自身属性;
- 调用父类构造函数,初始化父类属性并完成构造过程,此时构造过程已经完成;
- 为父类的属性按照子类的要求设置默认值,此时虽然是在构造函数中完成,但实质上已经在做构造过程之后的事情了,这时属性的修改就会触发属性观察器。
- Setting the value of properties that the subclass declares.
- Calling the superclass’s initializer.
- Changing the value of properties defined by the superclass. Any additional setup work that uses methods, getters, or setters can also be done at this point.”
Excerpt From: Apple Inc. “The Swift Programming Language (Swift 4.1).” iBooks.