接收错误“可选类型‘$T3’不能用作布尔值;改为测试‘!=nil’

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

我已经按照错误提示尝试了'!=nil'。但这也没有用。

下面是我的 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
        }

    }))

    }
}
if (!PFUser.currentUser()) { 

我认为这是问题所在。应该是

if PFUser.currentUser() == nil {

因为您正在测试不存在的值。请注意,您不需要在 swift 中将 if 条件括起来。