J Norris J Norris - 6 months ago 32
Swift Question

Type '()' does not conform to protocol 'Boolean Type'

This question has been asked a bunch of times, and I have looked at so many answers, but I can't get the answer because a) They asked it a long time ago and the code does not work anymore(updated) or b) It is not suited to my case.

Here's the code:

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
if() //your condition if user is already logged in or not
{
// 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


}

This is the code from the following thread : Show another view controller at the first launch and not again

I am trying to use this, but the error comes on the line of "if()". Has there been an update that gets rid of that? or was I just putting in wrong code?. It's in the right place (AppDelegate.swift).

If you need any more references or information, just comment below

Thank you in advance!

P.S: I would have just commented on his answer, but I don't have 50 points

kye kye
Answer

You if statement is not evaluating anything, that's why you're receiving that error. The code below is assuming you're saving the user's status in NSUserDefaults (userKey)

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
}