新的 Swift 2.2 / Xcode 7.3.1 的 AppDelegate 问题

Posted

技术标签:

【中文标题】新的 Swift 2.2 / Xcode 7.3.1 的 AppDelegate 问题【英文标题】:problems AppDelegate with new Swift 2.2 / Xcode 7.3.1 【发布时间】:2016-06-12 14:46:34 【问题描述】:

我最近升级了 Xcode 并尝试继续编程。我的应用程序无法构建。它说问题出在 AppDelegate 中。 我复制我的代码:

import UIKit


@UIApplicationMain
AppDelegate: UIResponder, UIApplicationDelegate 

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 
        // Override point for customization after application launch.

        let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
        let defaults = NSUserDefaults.standardUserDefaults()

        var rootViewController : UIViewController;

        if (defaults.boolForKey("HasBeenLaunched")) 
            // This gets executed if the app has ALREADY been launched
            rootViewController = storyboard.instantiateViewControllerWithIdentifier("maintabcontroller") as UIViewController
         else 
            // This gets executed if the app has NEVER been launched
            defaults.setBool(true, forKey: "HasBeenLaunched")
            defaults.synchronize()
            rootViewController = storyboard.instantiateViewControllerWithIdentifier("setupstory") as UIViewController
        

        window?.rootViewController = rootViewController;
        window?.makeKeyAndVisible();

        UITabBar.appearance().barTintColor = UIColor.whiteColor()

        UITabBar.appearance().tintColor = UIColor.blackColor()
        return true
    

错误在AppDelegate: UIResponder, UIApplicationDelegate 行中。它们是:

预期声明 语句不能以闭包表达式开头 不允许在顶层使用表达式 大括号语句块未使用 表达式解析为未使用的函数

在升级之前,我没有遇到所有这些错误。

【问题讨论】:

您在AppDelegate: UIResponder, UIApplicationDelegate 之前缺少一个“class 您可以创建一个新的空项目并查看您的项目中缺少什么。 【参考方案1】:

您不小心删除了 class 关键字:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate 

【讨论】:

我试过了,还是不行。我该如何解决?

以上是关于新的 Swift 2.2 / Xcode 7.3.1 的 AppDelegate 问题的主要内容,如果未能解决你的问题,请参考以下文章