属性'self.title'未在swift中的super.init调用中初始化

Dea*_*Lee 6 initialization uikit ios swift

我有一个派生自UIView的类,但我初始化它总是显示错误说"属性'self.title'未在swift中的super.init调用中初始化"

这是我的代码

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
}
}
Run Code Online (Sandbox Code Playgroud)

小智 8

init(frame :  CGRect ,title : String, recordUrl : String  , content : String) {
    self.title = title
    self.recordUrl = recordUrl
    self.content = content
    super.init(frame: frame)
 }
Run Code Online (Sandbox Code Playgroud)

在swift中,你应该首先初始化你的参数,然后实现super.Method()

  • 这在swift 3中不起作用. (3认同)
  • 这似乎不再起作用了。这会引发如下错误:“self”在“self.init”调用之前在属性访问“title”中使用” (2认同)