//存储属性
定义一般的常量或者变量,不需要重写set get方法
//计算属性
set get
该属性一般不用来直接赋值,重写set,get方法,也可以只写get方法。在get set里面赋值会引起重复引用,崩溃
用法:
var
fullName:
String
{
get
{
return "\(index) + \(name) + \(describution)"
return "\(index) + \(name) + \(describution)"
}
set
{
}
}
不能在里面给
fullName赋值
set{
//self.fullName = "\(index) + \(name) + \(describution)"
//self.fullName = "\(index) + \(name) + \(describution)"
}
//监视属性
该属性的变化会引起其他属性的改变,用willSet didSet监视该属性值变化,以便改变其他属性
//
属性
textStr的变化会引起属性
firstLabel的显示
var
textStr:
String?
{
willSet
{
print ( "NewValue = \ ( newValue )" )
}
didSet {
firstLabel . text = self . textStr
}
print ( "NewValue = \ ( newValue )" )
}
didSet {
firstLabel . text = self . textStr
}
}
不能写成
set{
self .textStr = textStr
firstLabel.text = self .textStr
self .textStr = textStr
firstLabel.text = self .textStr
}
self.会引起循环调用set方法