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 count,current count is 0
count changed form 0 to 15
原文链接:https://www.f2er.com/swift/323219.html