我收到错误:
AppDelegate类没有初始值设定项
似乎无法理解为什么。我关注this tutorial。我是初学者,所以任何帮助都会非常感激!
我的代码:
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
}
}
答案 0 :(得分:3)
由于centerContainer
不是可选的,并且没有给出默认值,因此Swift期望初始化函数给它一个。这里有一些可能的解决方案:
nil
,否则事情就会中断。答案 1 :(得分:1)
尝试将centerContainer
属性更改为可选,如下所示:
var centerContainer: MMDrawerController!
因此,它将使用nil
初始化,这应足以消除您所遇到的错误。 (顺便说一句,您不应该因此更改其余代码。)
来自The Swift Programming Language本书:
在创建该类或结构的实例时,类和结构必须将所有存储的属性设置为适当的初始值。存储的属性不能处于不确定状态。
答案 2 :(得分:0)
使centerContainer
var可选,例如window
。在swift中,必须初始化所有属性。当您将var设为可选时,您将其初始化为nil
。
var centerContainer: MMDrawerController?