ios - 为什么它一直说密码不匹配?

标签 ios swift

它一直提示我“密码不匹配”,我看不出问题出在哪里。 如果我删除与密码匹配的代码,它就会崩溃。 我确定 IBOutlets 和 IBAction 已正确连接。

@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

鉴于您的其他代码,我怀疑 email 和其他代码是 UITextFields,而不是字符串。我怀疑你的意思是:

    let userEmail = email.text ?? ""
    let userPassword = password.text ?? ""
    let userRepeatPassword = repeatPassword.text ?? ""

关于ios - 为什么它一直说密码不匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57232968/

相关文章:

ios - 单击自定义 UITableViewCell 上的按钮会抛出 EXC_BAD_ACCESS

ios - 标签栏 Controller 中多个标签的一个导航 Controller

swift - 当 VC 不在 View 中时,如何停止 VC 功能运行? [Swift 3.0 Xcode]

iphone - 将对象存储到 NSMutableArray

objective-c - UITabbarItem BadgeValue 文本颜色

ios - UIWebView 以低质量的放大状态呈现网页

ios - 如何获取 ARKit 特征点的 UIKit 坐标?

arrays - 你能在不知道swift类型的情况下声明一个空数组吗

ios - XCode 错误/警告/如何破坏 XCode 缓存的问题

ios - 在 inputAccessoryView 上方插入 subview