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

时间:2022-10-23 15:38:17

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.

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

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).

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

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

它看起来就像代码离开查询函数一样,checkEmail的值丢失了。

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
}

}

1 个解决方案

#1


2  

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关键字,该关键字声明一个新的局部变量:

let checkEmail = User["emailVerified"] as Bool

#1


2  

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关键字,该关键字声明一个新的局部变量:

let checkEmail = User["emailVerified"] as Bool