-2

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
}

}

Schuey999
  • 4,706
  • 7
  • 21
  • 36
  • You have to move the declaration of checkEmail outside of query.getObjectInBackgroundWithId("vFu93HatwL") { , – Duyen-Hoa Dec 17 '14 at 13:44
  • That isn't the only problem here. getObjectInBackgroundWithId() is asynchronous, so the second log still wouldn't be correct because checkEmail wouldn't have a value at this point. – Mick MacCallum Dec 17 '14 at 13:48
  • Wait...stupid question I know, but how can I change the value of a variable....Xcode is given me a bunch of BS errors. I know, this is an easy concept (I can do it in Java).....and checkEmail is declared at the top of my code. – Schuey999 Dec 17 '14 at 13:50
  • 1
    It would be helpful if you included what errors you are receiving and where. And you may have declared a property checkEmail, but you're creating a new local variable of the same name, which takes precedence over the property in the local scope. – Mick MacCallum Dec 17 '14 at 13:53
  • Never mind about the BS errors, I figured it out, I just put self. in front of the variable names. Anyways, back on topic, even when I remove the local declaration of the variable, I still loose the data. – Schuey999 Dec 17 '14 at 13:56

1 Answers1

3

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
David Berry
  • 40,941
  • 12
  • 84
  • 95
  • doesn't work. I get an error saying I need to add "self.". When I do that, it still doesn't fix the problem. – Schuey999 Dec 17 '14 at 20:10