//定义一个变量
var _tittle: String?
var tittle: String?{
set{
_tittle=newValue
}
get{
return _tittle
}
}
如果只从写get方法,会默认为readOnly
var age: Int?{
return 20
}
给age赋值会报错
在Swift语言中用了willSet和didSet这两个特性来监视属性的除初始化之外的属性值变化
var name: String?{
willSet{
//
NSLog("==========")
}
didSet{
NSLog("did set " + name!)
}
}
和OC相比较,我们可以在didSet里面执行一些改变UI的操作。
本文介绍Swift语言中如何使用willSet和didSet属性观察者来监控属性的变化,并对比Objective-C,说明如何利用这些特性进行UI更新。
52

被折叠的 条评论
为什么被折叠?



