Search code examples
objectswiftparse-platformbooleandata-retrieval

How to update the value of a boolean variable in swift?


I've spent the past 3 days trying to figure this out. I can easily do what I want to do in Java but as soon as I try to do it in Swift, Xcode gives me a hard time.

In this case, I am trying to retrieve a boolean object from Parse that will tell me whether the user's email has been verified. For some reason, whenever I tell to code to check to see if the object is false, checkEmail is apparently nil. Also worth noting, if I println(checkEmail) right after var checkEmail = User["emailVerified"] as Bool I get the correct boolean value (true or false).

Its looks like as soon as the code leaves the query function, the value for checkEmail is lost.

Any ideas on what I'm doing wrong?

import UIKit

class RegisterEmail: UIViewController {

var checkEmail: Bool?

override func viewDidLoad() {
    super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


 override func shouldPerformSegueWithIdentifier(identifier: String!, sender: AnyObject!) -> Bool {
    if identifier == "passEmail" {

        var query = PFUser.query()
        query.getObjectInBackgroundWithId("vFu93HatwL") {
            (User: PFObject!, error: NSError!) -> Void in
            if error == nil {
                NSLog("%@", User)
            } else {
                NSLog("%@", error)
            }

            let checkEmail = User["emailVerified"] as Bool


            println(checkEmail) //I get the correct value here

        }   

        println(checkEmail) //The value is lost here

        if (checkEmail == false) {

            let alert = UIAlertView()
            alert.title = "Error"
            alert.message = "The email you have provided has not been verified."
            alert.addButtonWithTitle("Dismiss")
            alert.show()

            return false
        }

        else {

            return true
        }
    }

    // by default, transition
    return true
}

}


Solution

  • You're not assigning the new value to the object property, you're assigning it to a local variable. Get rid of the let keyword, which declares a new local variable, in:

    let checkEmail = User["emailVerified"] as Bool