Ios name textfield变为强制性,然后UIAlert停止工作

Ios name textfield变为强制性,然后UIAlert停止工作,ios,swift,firebase,firebase-authentication,Ios,Swift,Firebase,Firebase Authentication,我添加了一行代码,以便在firebase中注册帐户时,名称文本字段是必需的,但当我这样做时,UIAlert中断。当我添加那行代码时,它就不再显示了。我添加的代码以>突出显示。解决问题的最佳方法是什么?重新编码必填名称文本字段或重新编码UIAlert。哪种方法最简单 @IBAction func registerTapped(_ sender: Any) { let namec = nameTextField.text if let email =

我添加了一行代码,以便在firebase中注册帐户时,名称文本字段是必需的,但当我这样做时,UIAlert中断。当我添加那行代码时,它就不再显示了。我添加的代码以>突出显示。解决问题的最佳方法是什么?重新编码必填名称文本字段或重新编码UIAlert。哪种方法最简单

        @IBAction func registerTapped(_ sender: Any) {
        let namec = nameTextField.text
         if let email = emailTextField.text, let pass = passwordTextField.text, let name = (namec?.capitalized.isEmpty)! ? nil:namec?.capitalized {
            FIRAuth.auth()?.createUser(withEmail: email, password: pass, completion: { (user, error) in
                if user != nil {
                    //user found

                    let interval = NSDate().timeIntervalSince1970
                    let date = Date(timeIntervalSince1970: interval)
                    let dateFormatter = DateFormatter()
                    dateFormatter.dateFormat = "dd/MM/yyyy/HH/mm/SS"
                    // you can change the date format to whatever you wants
                    let dateString = dateFormatter.string(from: date)
                    print(dateString)
                    self.refD?.child("Users").child((user?.uid)!).setValue(["Email": email, "Name": name, "User Created": dateString])
                    print("User Created And Added To Database", email, name, dateString)
                    self.performSegue(withIdentifier: "registertologin", sender: self)
                }
                else {
                    print(error!)
                    let alert = UIAlertController(title: "Error Creating Account ", message: "\(error!.localizedDescription)", preferredStyle: UIAlertControllerStyle.alert)
                    alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default, handler: nil))
                    self.present(alert, animated: true, completion: nil)
                }
            })
        }
    }
}

我认为您必须在主队列块中添加警报代码,因为您的代码位于完成处理程序块中

DispatchQueue.main.async {
    print(error!)
    let alert = UIAlertController(title: "Error Creating Account ", message: "\(error!.localizedDescription)", preferredStyle: UIAlertControllerStyle.alert)
    alert.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default, handler: nil))
    self.present(alert, animated: true, completion: nil)
}

试试这个

这是可行的,但是在名字文本字段中必须有文本才能弹出是的,你只需要把它放在你的else条件下