添加子视图时出现预期的声明错误

时间:2015-06-15 01:23:03

标签: ios swift xcode6

当我尝试将子视图添加到我的UIView时,我收到了“预期声明”错误,这是代码:

@IBDesignable
class TimePickerView: UIView {

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
    // Drawing code
}
*/


    var borderWid = CGFloat(3.0)
    let square = UIView()
    square.frame = CGRect(x: 30, y: 30, width: 50, height: 50) //line of error
    square.layer.borderColor = UIColor.blueColor().CGColor
    square.layer.borderWidth = borderWid
    self.view.addSubview(square)

}

1 个答案:

答案 0 :(得分:0)

您可以在任何函数之外的类中声明实例变量/属性(并指定默认值),但不能包含任何其他代码。

let square = UIView()之后的所有代码行都需要进入函数,可能是初始化函数。