如何在swift中更新布尔变量的值?

时间:2014-12-17 13:38:31

标签: object swift parse-platform boolean data-retrieval

我花了3天的时间试图解决这个问题。我可以很容易地用Java做我想做的事情,但是当我尝试在Swift中做到这一点时,Xcode给了我很多时间。

在这种情况下,我试图从Parse中检索一个布尔对象,它将告诉我用户的电子邮件是否已经过验证。出于某种原因,每当我告诉代码检查对象是否为假时,checkEmail显然是零。另外值得注意的是,如果我在println(checkEmail)之后var checkEmail = User["emailVerified"] as Bool,我会得到正确的布尔值(true或false)。

看起来,只要代码离开查询函数,checkEmail的值就会丢失。

关于我做错的任何想法?

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
}

}

1 个答案:

答案 0 :(得分:3)

您没有将新值分配给对象属性,而是将其分配给局部变量。在:

中删除声明新局部变量的let关键字
let checkEmail = User["emailVerified"] as Bool