【发布时间】:2015-02-02 17:34:53
【问题描述】:
下面的代码出现Property self.color not initialized as super.init call 错误。请问如何正确覆盖 init(frame:) 函数?我想将 color 与 init 调用一起传递。
class CircleView: UIView {
// properties
let color: UIColor
init(frame: CGRect, color: UIColor) {
self.color = color
super.init(frame: frame)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func drawRect(rect: CGRect) {
...
}
}
【问题讨论】:
-
查看stackoverflow.com/questions/24021093/… 另外,可能需要尝试使用 var 而不是 let。也可以设置颜色的默认值。
-
将
let color: UIColor更改为var color: UIColor! -
这两个技巧都有效。我正在服用@bbarnhart。谢谢你。很高兴接受作为答案..
-
我相信
init(frame:)方法很好。错误应该在init(coder:)方法中。在这里,您在初始化color属性之前调用super.init。
标签: ios swift initialization