【问题标题】:Swift 4 local variable value assignmentSwift 4 局部变量赋值
【发布时间】:2018-03-26 14:59:53
【问题描述】:

我正在尝试从 firebase 数据库中恢复一个值并将其与 UITextField 值进行比较,如果匹配,我将其保存到我将使用的 var 中。问题是,当我使用它时,有问题的变量有一个默认值。

上面我展示了我的 func 代码,其中受影响的变量是“codeRecovered”:

     @IBAction func signUpAction(_ sender: AnyObject)

    {
        var codeRecovered: String = ""
        if emailSignUpTextField.text == "" || self.secretCodeTextField.text == "" {

            let alertController = UIAlertController(title: "Error", message: "Please enter your email, pin code and password", preferredStyle: .alert)
            let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
            alertController.addAction(defaultAction)
            present(alertController, animated: true, completion: nil)

        } else {

            self.dbHandler = self.ref?.child("Companies").observe(.value, with: { (snapshot) in
                for child in snapshot.children {
                    let snap = child as! DataSnapshot
                    let value = snap.value as! [String:String]
                    if let auxSecretCode = value["secretCode"]
                    {
                        if auxSecretCode == self.secretCodeTextField.text{
                            print("Value recovered OK(works fine): \(auxSecretCode)")
                            codeRecovered = auxSecretCode
                            print("Recovered value saved OK(works fine): \(codeRecovered)")
                        }
                    }

                }

            })
    //Here codeRecovered is already ""
                print("\(codeRecovered) is the recovered value(empty) and \(self.secretCodeTextField.text ?? "def") is the textField value")
                if codeRecovered != self.secretCodeTextField.text{
                    let alertController = UIAlertController(title: "Error", message: "Please enter a correct pin code", preferredStyle: .alert)

                    let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
                    alertController.addAction(defaultAction)
                    present(alertController, animated: true, completion: nil)

                }
 //....

【问题讨论】:

  • 没有注意到你有同步我认为问题是默认值,确保对 firebase 的调用有延迟尝试将 ckeck if 语句移动到回调中或将其代码放入函数中叫它

标签: ios swift firebase swift4


【解决方案1】:

使用同步结果的异步调用....

 @IBAction func signUpAction(_ sender: AnyObject)

{
    var codeRecovered: String = ""
    if emailSignUpTextField.text == "" || self.secretCodeTextField.text == "" {

        let alertController = UIAlertController(title: "Error", message: "Please enter your email, pin code and password", preferredStyle: .alert)
        let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
        alertController.addAction(defaultAction)
        present(alertController, animated: true, completion: nil)

    } else {

        self.dbHandler = self.ref?.child("Companies").observe(.value, with: { (snapshot) in
            for child in snapshot.children {
                let snap = child as! DataSnapshot
                let value = snap.value as! [String:String]
                if let auxSecretCode = value["secretCode"]
                {
                    if auxSecretCode == self.secretCodeTextField.text{
                        print("Value recovered OK(works fine): \(auxSecretCode)")
                        codeRecovered = auxSecretCode
                        print("Recovered value saved OK(works fine): \(codeRecovered)")
                    }
                }

            }

//Here codeRecovered is already ""
            print("\(codeRecovered) is the recovered value(empty) and \(self.secretCodeTextField.text ?? "def") is the textField value")
            if codeRecovered != self.secretCodeTextField.text{
                let alertController = UIAlertController(title: "Error", message: "Please enter a correct pin code", preferredStyle: .alert)

                let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
                alertController.addAction(defaultAction)
                present(alertController, animated: true, completion: nil)

            }                

        })

要按顺序使用您的codeRecovered,它必须在self.dbHandler = self.ref?.child("Companies").... 块内,因为它在异步线程中运行

【讨论】:

  • 我正在考虑,我会尝试评论结果,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-05
  • 2013-08-02
  • 2011-11-06
  • 2018-01-12
  • 2018-02-14
  • 1970-01-01
相关资源
最近更新 更多