简体   繁体   English

Swift 和 Firebase 错误:无法将“UITextField”类型的值转换为预期的参数类型“String”

[英]Swift and Firebase Error: Cannot convert value of type 'UITextField' to expected argument type 'String'

I'd like to start this off and apologize if I use incorrect terminology as I am extremely new to coding and I am picking up Swift as my first language.如果我使用不正确的术语,我想开始并道歉,因为我对编码非常陌生,而且我选择 Swift 作为我的第一语言。

I seem to have some issues with my reset password function that I am trying to create using Swift and Firebase and I keep getting the error "Cannot convert the value of type 'UITextField' to expected argument type 'String'" on the following line of code:我尝试使用 Swift 和 Firebase 创建的重置密码功能似乎有一些问题,并且在以下行中不断收到错误“无法将‘UITextField’类型的值转换为预期的参数类型‘String’”代码:

Auth.auth().sendPasswordReset(withEmail: email)

The reset call to reset password is part of the following IB action which resets the password based on an email entered into the UITextField and also checks to make sure it is a valid email address:重置密码的重置调用是以下 IB 操作的一部分,它根据输入到 UITextField 的电子邮件重置密码,并检查以确保它是有效的电子邮件地址:

@IBAction func resetButton(_ sender: Any) {
    if let email = emailTextField {
        Auth.auth().sendPasswordReset(withEmail: email)
            func isValidEmail(_ email: String) -> Bool {
                let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"

                let emailPred = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
                return emailPred.evaluate(with: email)
            }
        }
    }

There are two issues I am hoping you guys are able to help address:有两个问题希望大家帮忙解决:

1) How do I resolve this error I can't resolve? 1) 如何解决这个我无法解决的错误?

2) How can I code the UI to display a pop-up message to display "Please enter a valid email address" if a non-valid email address is entered into the UITextField? 2) 如果在 UITextField 中输入了无效的电子邮件地址,我如何编码 UI 以显示弹出消息以显示“请输入有效的电子邮件地址”?

Thank you guys for taking the time to read my post and I hope you are able to help with my issues!感谢大家花时间阅读我的帖子,我希望你们能够帮助我解决问题!

It looks like email is a UITextField .看起来email是一个UITextField

You need to do UITextField.text to get the text of a UITextField ;你需要做的UITextField.text获得的文本UITextField ; for you, it is email.text .对你来说,它是email.text

However, I would rename your UITextField to emailTextField instead of email .但是,我会将您的UITextField重命名为emailTextField而不是email

Clarification: You have 2 email identifiers within the same method.说明:您在同一方法中有 2 个email标识符。 if let email = emailTextField and func isValidEmail(_ email: String) -> Bool { . if let email = emailTextFieldfunc isValidEmail(_ email: String) -> Bool { . You need to change one of these for you to be able to appropriately work with these data sets.您需要更改其中之一,以便能够适当地处理这些数据集。

Basically you tried to put UITextField to the String field.基本上,您尝试将UITextField放入String字段。 Seems like emailTextField is a UITextField not a String .似乎emailTextFieldUITextField而不是String

What actually the code would be代码实际上是什么

@IBAction func resetButton(_ sender: Any) {
    if let email = emailTextField.text {
       if isValidEmail(email) {
           Auth.auth().sendPasswordReset(withEmail: email)
          } else {
             // Invalid email
        }
     }
 }

func isValidEmail(_ email: String) -> Bool {
      let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"

      let emailPred = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
      return emailPred.evaluate(with: email)
 }

First of all the function isValidEmail is not being executed inside the IBAction .首先,函数isValidEmail没有在IBAction中执行。 You have to call it explicitly regardless where the function is declared.无论函数在哪里声明,您都必须显式调用它。

The error is pretty clear: email / emailTextField is a text field not a string, you have to get the text from its text property.错误很明显: email / emailTextField是文本字段而不是字符串,您必须从其text属性中获取text

resetButton calls isValidEmail , on success it sends the string, on failure it presents an error message. resetButton调用isValidEmail ,成功时发送字符串,失败时显示错误消息。

@IBAction func resetButton(_ sender: Any) {
    guard let email = emailTextField.text else { return }            
    if isValidEmail(email) {
        Auth.auth().sendPasswordReset(withEmail: email)
    } else {
        let alertController = UIAlertController(title: "Email validation failed", message: "Please enter a valid email address", preferredStyle: .alert)
        let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
        alertController.addAction(okAction)
        self.present(alertController, animated: true, completion: nil)
    }
}


func isValidEmail(_ email: String) -> Bool {
    let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
    let emailPred = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
    return emailPred.evaluate(with: email)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SWIFT 4-无法转换&#39;UITextField!&#39;类型的值 到预期的参数类型“ Double” - SWIFT 4 - Cannot convert value of type 'UITextField!' to expected argument type 'Double' 无法转换“ [String]”类型的值? 到预期的参数类型&#39;String&#39;Swift - Cannot convert value of type '[String]?' to expected argument type 'String' Swift 无法在Swift 3中将字符串类型的值转换为预期的参数类型int - cannot convert value of type string to expected argument type int in swift 3 Swift - 无法转换“任何?”类型的值到预期的参数类型“字符串” - Swift - Cannot convert value of type 'Any?' to expected argument type 'String' 无法将“字符串”类型的值转换为预期的参数类型“ NSManagedObject” Swift - Cannot convert value of type 'String' to expected argument type 'NSManagedObject' Swift Swift:无法将值类型字符串转换为预期的参数类型nsmutablestring - Swift: Cannot convert value type string to expected argument type nsmutablestring "无法将 Substring 类型的值转换为预期的参数类型 String - Swift 4" - Cannot convert value of type Substring to expected argument type String - Swift 4 Swift错误:无法将“ArraySlice”类型的值转换为预期的参数类型 - Swift Error: Cannot convert value of type 'ArraySlice' to expected argument type Swift 错误,无法转换类型“绑定”的值<String?> &#39; 到预期的参数类型 &#39;绑定<String> &#39; - Swift error, Cannot convert value of type 'Binding<String?>' to expected argument type 'Binding<String>' 错误:无法将类型“ [String]”的值转换为预期的参数类型“ String?” - Error: Cannot convert value of type “[String]” to expected argument type “String?”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM