在Swift中,调用super.init时未初始化'self.title'属性

6

我有一个从UIView派生的类,但是每次我初始化时都会显示错误,提示"在swift中的super.init调用中未初始化属性'self.title'"

这是我的代码:

class A: UIView
{

var title : String
var recordUrl : String
var content : String

required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }

init(frame :  CGRect ,title : String, recordUrl : String  , content : String)
{
    super.init(frame: frame)

    self.title = title
    self.recordUrl = recordUrl
    self.content = content
}
}
1个回答

12
init(frame :  CGRect ,title : String, recordUrl : String  , content : String) {
    self.title = title
    self.recordUrl = recordUrl
    self.content = content
    super.init(frame: frame)
 }

在Swift中,您应该首先初始化参数,然后再实现super.Method()。


3
这在 Swift 3 中不起作用。 - user3674231
@snapchatdotcom,您能告诉我们在Swift 3中应该做些什么吗? - ArgaPK
这在Swift 4中有效。谢谢! :) - Ebru Güngör
2
这似乎不再起作用了。它会抛出一个像这样的错误:"self' used in property access 'title' before 'self.init' call"。 - rantanplan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接