2017-03-26 57 views
-1

我从这里下载了一个示例代码,用于位置跟踪应用程序https://www.pubnub.com/blog/2015-05-05-getting-started-ios-location-tracking-and-streaming-w-swift-programming-language/。我试图运行该应用程序,但在AppDelegate类中,我收到一个错误,说“类AppDelegate没有初始化器”。什么导致这个错误,我该如何解决它?类AppDelegate没有初始化器

import UIKit 

@UIApplicationMain 
class AppDelegate: UIResponder, UIApplicationDelegate { 

    // MARK: - Properties 
    //var window: UIWindow? 
    var window = UIWindow(frame: UIScreen.mainScreen().bounds) 

    // MARK: - App Life Cycle 

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

     // Adding a Navigation Controller and tool bar 
     self.window.rootViewController = UINavigationController(rootViewController: MainViewController(nibName: nil, bundle: nil)) 

     // Make window visible 
     self.window.makeKeyAndVisible() 

     return true 
    } 
} 
+1

以编程方式添加根视图控制器,实际上所有的代码你有'AppDelegate'吗?除了window之外还有其他的属性吗? – rmaddy

+0

是的,这就是源代码中的所有内容。 – user7770817

+0

请在问题中复制并粘贴编译器给您的* exact *错误。谢谢! –

回答

1

我将窗口设置为一个可选的值,没有默认值。就像你原先然后注释掉

var window: UIWindow? 

然后给窗口的值,当你启动应用

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
let viewController = ViewController(nibName: nil, bundle: nil) //ViewController = Name of your controller 
let navigationController = UINavigationController(rootViewController: viewController) 

self.window = UIWindow(frame: UIScreen.mainScreen().bounds) 
self.window?.rootViewController = navigationController 
self.window?.makeKeyAndVisible() 

return true 

}