Swift中观察者模式观察一个值的变化

前端之家收集整理的这篇文章主要介绍了Swift中观察者模式观察一个值的变化前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
class Obserber {
    var name:String?
    var count:Int = 0
    {
        willSet{
            print("Will set an new value \(newValue) to count,current count is \(count)")
        }
        didSet{
            print("count changed form \(oldValue) to \(count)")
            if count < 10
            {
                name = "name1"
            }else
            {
                name = "name2"
            }
        }
    }
}
var ob:Obserber = Obserber()
ob.count = 15

输出结果为:

Will set an new value 15 to countcurrent count is 0

count changed form 0 to 15

猜你在找的Swift相关文章