Search code examples
swiftparse-platformpfuser

Receiving Error "Optional type '$T3' cannot be used as a boolean; test for '!=nil' instead


I have tried the '!=nil' as suggested by the error. But that did not work either.

Below is the code in my TableViewController


func viewDidAppear(animated: Bool) {
if (!PFUser.currentUser()) {
    var loginAlert:UIAlertController = UIAlertController(title: "Sign Up / Log In", message: "Please Sign Up or Log In", preferredStyle: UIAlertControllerStyle.Alert)

    loginAlert.addTextFieldWithConfigurationHandler({
        textfield in
        textfield.placeholder = "Username"

    })

    loginAlert.addTextFieldWithConfigurationHandler({
        textfield in
        textfield.placeholder = "Password"
        textfield.secureTextEntry = true

    })

    loginAlert.addAction(UIAlertAction(title: "Log In", style: UIAlertActionStyle.Default, handler: {
        alertAction in
        let textFields:NSArray = loginAlert.textFields as NSArray
        let usernameTextfield:UITextField = textFields.objectAtIndex(0) as UITextField
        let passwordTextfield:UITextField = textFields.objectAtIndex(1) as UITextField

        PFUser.logInWithUsernameInBackground(usernameTextfield.text, password: passwordTextfield.text){
            (user:PFUser!, error:NSError!)-> Void in
        }

    }))

    }
}

Solution

  • if (!PFUser.currentUser()) { 
    

    I assume this is the problem line. It should be

    if PFUser.currentUser() == nil {
    

    Since you're testing for the value not being present. Note you don't require brackets round the if condition in swift.