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.
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:
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:
@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?
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?
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
.
You need to do UITextField.text
to get the text of a UITextField
; for you, it is email.text
.
However, I would rename your UITextField
to emailTextField
instead of email
.
Clarification: You have 2 email
identifiers within the same method. if let email = emailTextField
and 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. Seems like emailTextField
is a UITextField
not a 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
. 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.
resetButton
calls isValidEmail
, on success it sends the string, on failure it presents an error message.
@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)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.