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
}
}
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.
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.
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.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With