[英]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 = emailTextField
和func 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
.似乎
emailTextField
是UITextField
而不是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.