Swift使用block方式进行KVO的监听

2021-06-18  本文已影响0人  凯司机

// block方式的KVO

class PersonK: NSObject {
    @objc dynamic var age: Int = 0
    var observation: NSKeyValueObservation?
    override init() {
        super.init()
        observation = observe(\PersonK.age, options: .new) {
            (person, change) in
            print(change.newValue as Any)
        }
    }
}
var p = PersonK()
p.age = 32
p.setValue(30, forKey: "age")
上一篇下一篇

猜你喜欢

热点阅读