屬性觀察 (Property Observers) 是 Swift 中一個(gè)很特殊的特性,利用屬性觀察我們可以在當(dāng)前類型 內(nèi)監(jiān)視對(duì)于屬性的設(shè)定,并作出一些響應(yīng)。Swift 中為我們提供了兩個(gè)屬性觀察的方法,它們分別 是 willSet 和 didSet 。
使用這兩個(gè)方法十分簡(jiǎn)單,我們只要在屬性聲明的時(shí)候添加相應(yīng)的代碼塊,就可以對(duì)將要設(shè)定的
值和已經(jīng)設(shè)置的值進(jìn)行監(jiān)聽了:
class Test {
var value: Int = 100 {
willSet {
print("即將將原值 \(value) 設(shè)定至 \(newValue)") }
didSet {
print("已經(jīng)將原值 \(oldValue) 設(shè)定至 \(value)")
}
}
init() {
value = 111
}
}
let test = Test()
test.value = 10
// 即將將原值 100 設(shè)定至 10
// 已經(jīng)將原值 100 設(shè)定至 10
在 willSet 和 didSet 中我們分別可以使用 newValue 和 oldValue 來(lái)獲取將要設(shè)定的和已經(jīng)設(shè)定 的值。
但是通過(guò)上面例子可以發(fā)現(xiàn),在init方法中為屬性初始化,并沒(méi)有觸發(fā)屬性觀察,猜測(cè)是否是init執(zhí)行完屬性觀察才會(huì)生效
class Test {
var value: Int = 10
}
class Son: Test {
var age: Int {
didSet {
print("didset age")
}
willSet {
print("willset age")
}
}
override var value: Int {
didSet {
print("didset value")
}
willSet {
print("willset value")
}
}
override init() {
age = 10
super.init()
age = 100
value = 100
}
}
let test = Son()
// willset value
// didset value
可以發(fā)現(xiàn)只觸發(fā)了value 的屬性觀察,因?yàn)閍ge所屬類為Son,此時(shí)初始化方法并沒(méi)有結(jié)束,所以沒(méi)有觸發(fā)屬性觀察,而value所屬類為Test,此時(shí)通過(guò)super.init()已經(jīng)結(jié)束了對(duì)父類的初始化,所以會(huì)觸發(fā)
所以可以得出結(jié)論,當(dāng)屬性所屬類的初始化完成才會(huì)觸發(fā)屬性觀察
可以通過(guò)SIL觀察具體實(shí)現(xiàn)方式:
// Son.init()
sil hidden @main.Son.init() -> main.Son : $@convention(method) (@owned Son) -> @owned Son {
// %0 "self" // users: %10, %6, %3, %2
bb0(%0 : $Son):
%1 = alloc_stack $Son, let, name "self" // users: %19, %11, %3, %32, %33
strong_retain %0 : $Son // id: %2
store %0 to %1 : $*Son // id: %3
%4 = integer_literal $Builtin.Int64, 10 // user: %5
%5 = struct $Int (%4 : $Builtin.Int64) // user: %8
%6 = ref_element_addr %0 : $Son, #Son.age // user: %7
%7 = begin_access [modify] [dynamic] %6 : $*Int // users: %8, %9
store %5 to %7 : $*Int // id: %8
end_access %7 : $*Int // id: %9
strong_release %0 : $Son // id: %10
%11 = load %1 : $*Son // user: %12
%12 = upcast %11 : $Son to $Test // user: %14
// function_ref Test.init()
%13 = function_ref @main.Test.init() -> main.Test : $@convention(method) (@owned Test) -> @owned Test // user: %14
%14 = apply %13(%12) : $@convention(method) (@owned Test) -> @owned Test // user: %15
%15 = unchecked_ref_cast %14 : $Test to $Son // users: %31, %26, %22, %19, %18, %30, %29, %17, %34, %16
strong_retain %15 : $Son // id: %16
strong_retain %15 : $Son // id: %17
strong_retain %15 : $Son // id: %18
store %15 to %1 : $*Son // id: %19
%20 = integer_literal $Builtin.Int64, 100 // user: %21
%21 = struct $Int (%20 : $Builtin.Int64) // user: %24
%22 = ref_element_addr %15 : $Son, #Son.age // user: %23
%23 = begin_access [modify] [dynamic] %22 : $*Int // users: %24, %25
store %21 to %23 : $*Int // id: %24
end_access %23 : $*Int // id: %25
strong_release %15 : $Son // id: %26
%27 = integer_literal $Builtin.Int64, 100 // user: %28
%28 = struct $Int (%27 : $Builtin.Int64) // user: %30
%29 = class_method %15 : $Son, #Son.value!setter : (Son) -> (Int) -> (), $@convention(method) (Int, @guaranteed Son) -> () // user: %30
%30 = apply %29(%28, %15) : $@convention(method) (Int, @guaranteed Son) -> ()
strong_release %15 : $Son // id: %31
destroy_addr %1 : $*Son // id: %32
dealloc_stack %1 : $*Son // id: %33
return %15 : $Son // id: %34
} // end sil function 'main.Son.init() -> main.Son'
// 為age初始化直接調(diào)用了 store,并沒(méi)有調(diào)用setter 方法


// 為value初始化 調(diào)用了 setter 方法,而didSet,willSet就是通過(guò)setter方法觸發(fā)的
