Class 'ViewController' has no initializers in swift

SwiftXcode6Swift Playground

Swift Problem Overview


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.

Swift Solutions


Solution 1 - Swift

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.

Solution 2 - Swift

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
    }
    
}

Solution 3 - Swift

Sometimes this error also appears when you have a var or a let that hasn't been intialized.

For example

class ViewController: UIViewController {
    var x: Double
    // or
    var y: String
    // or
    let z: Int
}

Depending on what your variable is supposed to do you might either set that var type as an optional or initialize it with a value like the following

class ViewController: UIViewCOntroller {
    // Set an initial value for the variable
    var x: Double = 0
    // or an optional String
    var y: String?
    // or
    let z: Int = 2
}

Solution 4 - Swift

This issue usually appears when one of your variables has no value or when you forget to add "!" to force this variable to store nil until it is set.

In your case the problem is here:

var delegate: AppDelegate

It should be defined as var delegate: AppDelegate! to make it an optional that stores nil and do not unwrap the variable until the value is used.

It is sad that Xcode highlights the whole class as an error instead of highlighting the particular line of code that caused it, so it takes a while to figure it out.

Solution 5 - Swift

if you lost a "!" in your code ,like this code below, you'll also get this error.

import UIKit

class MemeDetailViewController : UIViewController {

    @IBOutlet weak var memeImage: UIImageView!

    var meme:Meme! // lost"!"

    override func viewWillAppear(animated: Bool) {
    
        super.viewWillAppear(animated)
        self.memeImage!.image = meme.memedImage
    }

    override func viewDidDisappear(animated: Bool) {
        super.viewDidDisappear(animated)
    }

}

Solution 6 - Swift

Replace var appDelegate : AppDelegate? with let appDelegate = UIApplication.sharedApplication().delegate as hinted on the second commented line in viewDidLoad().

The keyword "optional" refers exactly to the use of ?, see this for more details.

Solution 7 - Swift

I use Xcode 7 and Swift 2. Last, I had made:

class ViewController: UIViewController{ var time: NSTimer //error this here }

Then I fix: class ViewController: UIViewController {

var time: NSTimer!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func viewWillAppear(animated: Bool) {
    //self.movetoHome()
    time = NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: #selector(ViewController.movetoHome), userInfo: nil, repeats: false)
    //performSegueWithIdentifier("MoveToHome", sender: self)
    //presentViewController(<#T##viewControllerToPresent: UIViewController##UIViewController#>, animated: <#T##Bool#>, completion: <#T##(() -> Void)?##(() -> Void)?##() -> Void#>)
}

func movetoHome(){
    performSegueWithIdentifier("MoveToHome", sender: self)
}

}

Solution 8 - Swift

For me was a declaration incomplete. For example:

var isInverted: Bool

Instead the correct way:

var isInverted: Bool = false

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontranvutuanView Question on Stackoverflow
Solution 1 - SwiftdrewagView Answer on Stackoverflow
Solution 2 - SwiftImanou PetitView Answer on Stackoverflow
Solution 3 - SwiftOliver SView Answer on Stackoverflow
Solution 4 - SwiftIvan VView Answer on Stackoverflow
Solution 5 - SwiftsaneryeeView Answer on Stackoverflow
Solution 6 - Swiftuser2118161View Answer on Stackoverflow
Solution 7 - SwiftAmyNguyenView Answer on Stackoverflow
Solution 8 - SwiftAlessandro MattiuzziView Answer on Stackoverflow