我尝试在类的扩展上声明IBOutlet属性.但它给出错误
‘var’ declaration without getter/setter method not allowed here
class ExampleView : UIView
{
}
extension ExampleView
{
@IBOutlet var btn1,btn2 : UIButton // here I got error.
}
请任何人建议我正确的做法吗?
解决方法
来自扩展 – > Swift编程语言中的计算属性
NOTE
Extensions can add new computed properties,but they cannot add stored
properties,or add property observers to existing properties.
另外响应twlkyao的评论:这是我的一个Double的absoluteValue属性的实现
extension Double {
var absoluteValue: Double {
if self >= 0 {
return self
} else {
return -self
}
}
}
// Simple test -> BOTH println() should get called.
var a = -10.0
if (a < 0) {
println("Smaller than Zero")
}
if (a.absoluteValue > 5) {
println("Absolute is > 5")
}