3

I'm receiving the error:

Class AppDelegate has no initializers

and cannot seem to understand why. I am following this tutorial. I am a beginner so any help would be VERY appreciated!

My code:

import UIKit

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    var centerContainer: MMDrawerController

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        _ = self.window!.rootViewController

        let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)

        let centerViewController = mainStoryboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController

        let leftViewController = mainStoryboard.instantiateViewController(withIdentifier: "LeftSideViewController") as! LeftSideViewController

        let leftSideNav = UINavigationController(rootViewController: leftViewController)
        let centerNav = UINavigationController(rootViewController: centerViewController)


        centerContainer = MMDrawerController(center: centerNav, leftDrawerViewController: leftSideNav)


        centerContainer.openDrawerGestureModeMask = MMOpenDrawerGestureMode.panningCenterView;

        centerContainer.closeDrawerGestureModeMask = MMCloseDrawerGestureMode.panningCenterView;

        window!.rootViewController = centerContainer
        window!.makeKeyAndVisible()

        return true
    }
}
Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
Acer
  • 73
  • 1
  • 3
  • Does `centerContainer` need to be a property? Will it be access from other code besides the `didFinishLaunchingWithOptions` method? – rmaddy May 05 '17 at 16:36
  • 2
    Possible duplicate of [Class has no initializers Swift](http://stackoverflow.com/questions/27797351/class-has-no-initializers-swift) – Nazmul Hasan May 05 '17 at 16:51

3 Answers3

8

Try changing your centerContainer property to an optional like this:

var centerContainer: MMDrawerController!

as such, it will be initialized with nil, which should be enough to remove the error you are having. (By the way, you shouldn't need to change the rest of your code as a result.)

From The Swift Programming Language book:

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.

Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
4

Since centerContainer isn't an optional and isn't given a default value, Swift expects an initializer function to give it one. There are a few possible solutions here:

  1. Give it a default value. Probably not practical in this context.
  2. Make it optional. Would work, but depending on how it's used might add a lot of unnecessary unwrapping to your code.
  3. Make it an implicitly unwrapped optional. This is essentially saying "I can't/don't want to give this a default value or set it in an initializer, but I will definitely be giving it a value before I use it for anything." Usually the cleanest option for things like this, just be careful that you don't assign it nil at any point later in your code or things will break.
John Montgomery
  • 6,739
  • 9
  • 52
  • 68
0

Make your centerContainer var optional like window. In swift, all of the properties must be initialized. When you make your var optional, you are initializing it as nil.

var centerContainer: MMDrawerController?
nayem
  • 7,285
  • 1
  • 33
  • 51
  • Using an *implicitly unwrapped* optional here (ie, `MMDrawerController!`) might be a better fit here. See my answer below. – Paulo Mattos May 05 '17 at 16:38
  • @PauloMattos Yes! That's absolutely right. But as the OP had some basic knowledge on `optional` (he had used one as `window`) I hope _not bragging some more concept_(that is, `implicit optional`) would be good for him. – nayem May 05 '17 at 16:41
  • 1
    Good point man, I agree! A *concepts overload* -- a common behavior on SO, one may say -- might scary him away. Sometimes you need to learn a trick at time :) – Paulo Mattos May 05 '17 at 16:46