类型'()'不符合协议'布尔类型'

时间:2016-06-04 00:54:57

标签: swift boolean

这个问题已经被问了很多次,我看了很多答案,但是我找不到答案因为a)他们很久以前问了这个问题,代码不再适用了(更新的)或b)它不适合我的情况。

以下是代码:

'Access-Control-Allow-Origin':'*',
'Access-Control-Allow-Methods' : 'POST, GET, PUT, DELETE',
'Access-Control-Allow-Headers' : 'Content-Type, Authorization, Content-Length, X-Requested-With'

}

这是以下主题中的代码:Show another view controller at the first launch and not again

我正在尝试使用它,但错误出现在“if()”行。有没有更新摆脱它?还是我只是输错了代码?它位于正确的位置(AppDelegate.swift)。

如果您需要更多参考或信息,请在下面发表评论

提前谢谢!

P.S:我会对他的回答发表评论,但我没有得到50分

1 个答案:

答案 0 :(得分:1)

if声明未评估任何内容,这就是您收到该错误的原因。以下代码假设您在NSUserDefaultsuserKey

中保存了用户的状态
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {     

//SET INITIAL CONTROLLER
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    var initialViewController: UIViewController

   //retrive the user key or status from NSUserDefaults here
    let defaults = NSUserDefaults.standardUserDefaults()
    if let userKey = defaults.stringForKey("userKey"){
       // if already logged in then redirect to MainViewController

        initialViewController =     mainStoryboard.instantiateViewControllerWithIdentifier("MainController") as! MainViewController // 'MainController' is the storyboard id of MainViewController 
    }
    else
    {
       //If not logged in then show LoginViewController
        initialViewController = mainStoryboard.instantiateViewControllerWithIdentifier("LoginController") as! LoginViewController // 'LoginController' is the storyboard id of      LoginViewController 

    }

    self.window?.rootViewController = initialViewController

    self.window?.makeKeyAndVisible()
 return true
}