Search code examples
iosswiftparse-platformverification

Checking user verification using '==' on a Bool throws error using Swift and Parse


Im trying to check if there is a user and if they have verified their account (by phone number) and if there is and they have then let them continue but if there is a user that has not verified their phone number segue them to verify their phone number. My problem is that i set "phoneNumberVerified" in the Parse User class as a Bool but then I try to check using an if statement I get the error: Binary operator '==' cannot be applied to operands of type 'AnyObject!' and 'Bool'

Code to check user and verification status:

override func viewDidLoad() {
    super.viewDidLoad()

    var currentUser = PFUser.currentUser()

    if currentUser == nil || currentUser!["phoneNumberVerified"] == nil {

        performSegueWithIdentifier("showInitialView", sender: self)

    } else {

        if let numberIsVerified = currentUser!["phoneNumberVerified"] as? Bool {

            if currentUser != nil && numberIsVerified == false {
                performSegueWithIdentifier("showVerifyUserView", sender: self)

            } else if currentUser != nil && numberIsVerified == true {
                performSegueWithIdentifier("showMyPostsView", sender: self)

            } else {
                performSegueWithIdentifier("showInitialView", sender: self)
            }
        }
    }
}

}


Solution

  • I think you should just try and downcast it as a Bool

    override func viewDidLoad() {
        super.viewDidLoad()
    
        if let currentUser = PFUser.currentUser() {
    
            if let numberIsVerified = currentUser["phoneNumberVerified"] as? Bool {
    
                if numberIsVerified == false {
                    performSegueWithIdentifier("showVerifyUserView", sender: self)
    
                } else if numberIsVerified == true {
                    performSegueWithIdentifier("showMyPostsView", sender: self)
    
                } else {
                    performSegueWithIdentifier("showInitialView", sender: self)
                }
            }
        }
    }