swift delegate

前端之家收集整理的这篇文章主要介绍了swift delegate前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
protocol ModelDelegate {
    func willValueChanged(curValue: Int,newValue: Int)
    func didValueChanged(curValue: Int,oldValue: Int)
}

class Model {
    var delegate: ModelDelegate?
    
    var count: Int = 0 {
        willSet {
            delegate?.willValueChanged(count,newValue: newValue)
        }
        didSet {
            delegate?.didValueChanged(count,oldValue: oldValue)
        }
    }
}

class View: ModelDelegate {
    func willValueChanged(curValue: Int,newValue: Int) {
        print("will,cur:\(curValue),new:\(newValue)")
    }
    
    func didValueChanged(curValue: Int,oldValue: Int) {
        print("did,old:\(oldValue)")
    }
}

let model = Model()
let view = View()
model.delegate = view
model.count = 100
model.count = 200

猜你在找的Swift相关文章