ios – Swift:在这里不允许使用没有getter / setter方法的’var’声明

我尝试在类的扩展上声明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")
}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...