Ios 为什么它总是说密码不是';不匹配?
它不断向我显示“密码不匹配”警报,我看不出问题出在哪里。 如果我删除与密码匹配的代码,它就会崩溃。 我确信IB插座和IBAction已正确连接Ios 为什么它总是说密码不是';不匹配?,ios,swift,Ios,Swift,它不断向我显示“密码不匹配”警报,我看不出问题出在哪里。 如果我删除与密码匹配的代码,它就会崩溃。 我确信IB插座和IBAction已正确连接 @IBAction func signupButton(_ sender: UIButton) { let userEmail = email let userPassword = password let userRepeatPassword = repeatPassword //
@IBAction func signupButton(_ sender: UIButton) {
let userEmail = email
let userPassword = password
let userRepeatPassword = repeatPassword
// Check empty fields
if (email.hasText == false) || (password.hasText == false) || (repeatPassword.hasText == false) {
alertMessage(userMessage: "All fields are required!")
return
}
// Check if passwords match
if repeatPassword != password {
alertMessage(userMessage: "Passwords do not match!")
return
}
// Store data
UserDefaults.standard.set(userEmail, forKey: "userEmail")
UserDefaults.standard.set(userPassword, forKey: "userPassword")
UserDefaults.standard.synchronize()
// Display alert message with confirmation
var alert = UIAlertController(title: "Alert", message: "Signed up!", preferredStyle: UIAlertController.Style.alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertAction.Style.default) { action in
self.dismiss(animated: true, completion: nil)
}
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
func alertMessage (userMessage: String) {
var alert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertController.Style.alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: nil)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
}
我怀疑这些说法是错误的:
let userEmail = email
let userPassword = password
let userRepeatPassword = repeatPassword
考虑到您的其他代码,我怀疑电子邮件
,其他代码是UITextFields,而不是字符串。我猜你的意思是:
let userEmail = email.text ?? ""
let userPassword = password.text ?? ""
let userRepeatPassword = repeatPassword.text ?? ""
我怀疑这些说法是错误的:
let userEmail = email
let userPassword = password
let userRepeatPassword = repeatPassword
考虑到您的其他代码,我怀疑电子邮件
,其他代码是UITextFields,而不是字符串。我猜你的意思是:
let userEmail = email.text ?? ""
let userPassword = password.text ?? ""
let userRepeatPassword = repeatPassword.text ?? ""