Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'self.title' not initialized at super.init call in swift [duplicate]

I have a class derived from UIView , but i initialized it always show the error says "Property 'self.title' not initialized at super.init call in swift"

Here is my code

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
}
}
like image 238
Dean Lee Avatar asked Jul 06 '16 04:07

Dean Lee


People also ask

Do you need to call super init in Swift?

The Swift Programming Language: If a subclass initializer performs no customization in phase 2 of the initialization process, and the superclass has a zero-argument designated initializer, you can omit a call to super. init() after assigning values to all of the subclass's stored properties.

What does super init mean in Swift?

super is just reference to superclass and that superclass have init method, so by calling super.init() you call init method of superclass without parameters. If init method of superclass have parameters class Animal { init(name: String) { } } you must pass parameters to this method class Cat: Animal { init() { super.

What is a property initializer Swift?

Swift provides a default initializer for any structure or class that provides default values for all of its properties and doesn't provide at least one initializer itself. The default initializer simply creates a new instance with all of its properties set to their default values.

What is initialize in Swift?

Swift init() Initialization is the process of preparing an instance of a class, structure, or enumeration for use. This process involves setting an initial value for each stored property on that instance and performing any other setup or initialization that is required before the new instance is ready for use.


1 Answers

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

In swift, you should init your parameter first, and then implement super.Method()

like image 65
Kim Jin Avatar answered Nov 14 '22 23:11

Kim Jin