Swift 添加观察者

addObserver形式

创建观察者并添加观察

class People: NSObject {
    // 在Swift中使用KVO来监听属性的变化要加上 @objc 和 dynamic 来修饰
    @objc dynamic var name:String?
    @objc dynamic var sex:String?
    
    init(name:String, sex:String = "M") {
        self.name = name
        self.sex = sex
        super.init()
        for keyPath in ["name", "sex"] {
            // 添加观察者
            addObserver(self, forKeyPath: keyPath, options: [.new, .old], context: nil)
        }
    }
    
    // 监听到值的变化
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        debugPrint("keyPath = \(keyPath!) old = \(change![NSKeyValueChangeKey.oldKey]!) new = \(change![NSKeyValueChangeKey.newKey]!)")
    }
    
    //  析构函数
    deinit {
        for keyPath in ["name", "sex"] {
            // 移除观察者
            removeObserver(self, forKeyPath: keyPath)
        }
    }
}

调用

        var  pp: People?
        pp  = People(name: "飞", sex: "W")
        pp?.name = "凤"
        pp?.sex = "👱"

输出结果

"keyPath = name old = 飞 new = 凤"
"keyPath = sex old = W new = 👱"

observe 形式

添加

class People: NSObject {
    // 在Swift中使用KVO来监听属性的变化要加上 @objc 和 dynamic 来修饰
    @objc dynamic var name:String?
    @objc dynamic var sex:String?
    
    var nameKeyValueObservation:NSKeyValueObservation?
    var sexKeyValueObservation:NSKeyValueObservation?

    init(name:String, sex:String = "M") {
        self.name = name
        self.sex = sex
        super.init()
        
        // 不指定NSKeyValueObservation, 观察器不起作用
        nameKeyValueObservation = observe(\People.name, options: [.new, .old], changeHandler: { (self, change) in
            debugPrint("newValue = \(String(describing: change.newValue)) oldValue = \(String(describing: change.oldValue))")
        })
        
        let _ = observe(\People.name, options: [.new, .old], changeHandler: { (self, change) in
            debugPrint("不起作用")
            debugPrint("newValue = \(String(describing: change.newValue)) oldValue = \(String(describing: change.oldValue))")
        })

        
        sexKeyValueObservation = observe(\.sex, options: [.new, .old], changeHandler: { (self, change) in
            debugPrint("newValue = \(String(describing: change.newValue)) oldValue = \(String(describing: change.oldValue))")
        })
    }
}

调用

        var  pp: People?
        pp  = People(name: "飞", sex: "W")
        pp?.name = "凤"
        pp?.sex = "👱"

输出

"newValue = Optional(Optional(\"凤\")) oldValue = Optional(Optional(\"飞\"))"
"newValue = Optional(Optional(\"👱\")) oldValue = Optional(Optional(\"W\"))"

属性观察器

添加

class People: NSObject {
    
    private var _name:String = ""
    private var _sex:String = ""
            
    var name:String = "" {
        willSet {
            debugPrint("newVaule = \(newValue)")
            _name = newValue
        }
        didSet {
            debugPrint("oldValue = \(oldValue)")
        }
    }
    
    var sex:String = "" {
        willSet {
            debugPrint("newVaule = \(newValue)")
            _sex = newValue
        }
        didSet {
            debugPrint("oldValue = \(oldValue)")
        }
    }
}

调用

        pp  = People()
        pp?.name = "🐉"
        pp?.sex = "W"

输出

"newVaule = 🐉"
"oldValue = "
"newVaule = W"
"oldValue = "

Tips

  1. Swift值Key-Path表达式:
    Key-Path表达式通常用来引用一个类型的属性和下标,可以在KVO等动态编程中使用 。基本形式为“\type name .path”; 类型名 是一个具体类型的名称包含泛型类型,路径可以右属性 ,下标,可选链式表达式和强制解包表达式构成。在编译期Key-Path表达式会被 keyPath类的实例代替
  2. OC是基于运行时的,遵循了KVC和Dynamic Dispatch。Swift为了性能考虑使用的是静态派发。
  3. Swift使用 dynamic修饰告诉编译期使用动态派发
posted @ 2021-10-15 15:27  jisa  阅读(298)  评论(0编辑  收藏  举报