为什么我无法使用Guard调出UIAlert函数

时间:2019-07-02 16:57:04

标签: ios swift xcode uialertcontroller

未调用警报功能,但没有错误

@objc func handleSignUp(){

   guard let email = emailTextField.text else {
        let alertemail = UIAlertController(title: "Error!", message: "Please enter a valid email address!", preferredStyle: UIAlertController.Style.alert)
        let actionemail = UIAlertAction(title: "OK", style: .default) { (actionemail) in
            alertemail.dismiss(animated: true, completion: nil)
        }
        alertemail.addAction(actionemail)
        self.present(alertemail, animated: true, completion: nil)
        return
    }
    guard let password = passwordTextField.text else {
        let alertpassword = UIAlertController(title: "Error!", message: "Please enter a valid password!", preferredStyle: UIAlertController.Style.alert)
        let actionpassword = UIAlertAction(title: "OK", style: .default) { (actionpassword) in
            alertpassword.dismiss(animated: true, completion: nil)
        }
        alertpassword.addAction(actionpassword)
        return
    }

    createUser(withEmail: email, password: password)
}

1 个答案:

答案 0 :(得分:0)

text对象的UITextField属性(与UILabel不同)从不nilThis string is @"" by default

您可以添加一个检查字符串是否为空

guard let email = emailTextField.text, !email.isEmpty else { ...

但是您应该验证电子邮件地址