Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class 'ViewController' has no initializers in swift

Getting the complaint from the compiler when I am doing this

class ViewController: UIViewController {      var delegate : AppDelegate     override func viewDidLoad() {         super.viewDidLoad()         // Do any additional setup after loading the view, typically from a nib.         //self.appDelegate = UIApplication.sharedApplication().delegate;      }      @IBAction func getData(sender : AnyObject) {      }      @IBAction func LogOut(sender : AnyObject) {     } } 

However, if I just add ? at the end of AppDelegate like below and the error is gone.

class ViewController: UIViewController {      var delegate : AppDelegate?     override func viewDidLoad() {         super.viewDidLoad()         // Do any additional setup after loading the view, typically from a nib.         //self.appDelegate = UIApplication.sharedApplication().delegate;      }      @IBAction func getData(sender : AnyObject) {      }      @IBAction func LogOut(sender : AnyObject) {     } } 

I don't see optional keyword relevant to this error unless I am wrong.

like image 352
tranvutuan Avatar asked Sep 12 '14 15:09

tranvutuan


People also ask

Has no initializers Swift?

Every Swift developer runs into this error at some point. To resolve this error, you need to understand an important aspect of classes. Swift requires that the stored properties of a class receive an initial value during initialization.

What are Initializers in Swift?

An initializer is a special type of function that is used to create an object of a class or struct. In Swift, we use the init() method to create an initializer.

What is convenience init Swift?

Convenience initializers are secondary, supporting initializers for a class. You can define a convenience initializer to call a designated initializer from the same class as the convenience initializer with some of the designated initializer's parameters set to default values.


2 Answers

The error could be improved, but the problem with your first version is you have a member variable, delegate, that does not have a default value. All variables in Swift must always have a value. That means that you have to set it up in an initializer which you do not have or you could provide it a default value in-line.

When you make it optional, you allow it to be nil by default, removing the need to explicitly give it a value or initialize it.

like image 159
drewag Avatar answered Dec 05 '22 04:12

drewag


The Swift Programming Language states:

Classes and structures must set all of their stored properties to an appropriate initial value by the time an instance of that class or structure is created. Stored properties cannot be left in an indeterminate state.

You can set an initial value for a stored property within an initializer, or by assigning a default property value as part of the property’s definition.

Therefore, you can write:

class myClass {      var delegate: AppDelegate //non-optional variable      init() {         delegate = UIApplication.sharedApplication().delegate as AppDelegate     }  } 

Or:

class myClass {      var delegate = UIApplication.sharedApplication().delegate as AppDelegate //non-optional variable      init() {         println("Hello")     }  } 

Or:

class myClass {      var delegate : AppDelegate! //implicitly unwrapped optional variable set to nil when class is initialized      init() {         println("Hello")     }      func myMethod() {         delegate = UIApplication.sharedApplication().delegate as AppDelegate     }  } 

But you can't write the following:

class myClass {      var delegate : AppDelegate //non-optional variable      init() {         println("Hello")     }      func myMethod() {         //too late to assign delegate as an non-optional variable         delegate = UIApplication.sharedApplication().delegate as AppDelegate     }  } 
like image 20
Imanou Petit Avatar answered Dec 05 '22 06:12

Imanou Petit