首页 > 代码库 > Swift-属性监听

Swift-属性监听

监听属性的改变(开发中使用很多)

oc中长是重写set方法

swift通过属性监听器

 

class Dog: NSObject {    var name:String?{//        属性监听器//        属性即将改变监听        willSet{//            print("111")//            print(name)            print(newValue)        }//        属性已经改变监听//        开发中常用这个监听属性改变        didSet{//            print("222")//            print(name)            print(oldValue)        }    }    }let d = Dog()d.name = "why"//d.name = "ee"print(d.name)

 

Swift-属性监听