首页 > 解决方案 > Swift - If 语句不会运行

问题描述

我的if声明不会运行。我不知道为什么它不会运行。

@IBOutlet weak var Email: UITextField!
@IBOutlet weak var Password: UITextField!
@IBAction func Login(_ sender: AnyObject) {      
    let email = Email.text
    let password = Password.text

    let postURL = URL(string: "https://www.example.com/app/signin.php?email" + email! + "&password" + password!)!
    var postRequest = URLRequest(url: postURL)
    postRequest.httpMethod = "POST"
    postRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
    postRequest.setValue("application/json", forHTTPHeaderField: "Accept")

    let httpBODY = postRequest.httpBody
    let mail : Data = email!.data(using: .utf8)!
    if httpBODY == mail { // Error is happening right here
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewController(withIdentifier: "LoggedOn")
            self.navigationController?.pushViewController(vc, animated: true)
        } else {
            let title = "Wrong!"
            let message = "Incorrect username or bad Password."

            let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)

            let OKAction = UIAlertAction(title: "OK", style: .default)
            alertController.addAction(OKAction)

            var alertWindow : UIWindow!
            alertWindow = UIWindow.init(frame: UIScreen.main.bounds)
            alertWindow.rootViewController = UIViewController.init()
            alertWindow.windowLevel = UIWindowLevelAlert + 1
            alertWindow.makeKeyAndVisible()
            alertWindow.rootViewController?.present(alertController, animated: true)
        }
    }

预期:Button 将获取电子邮件和密码并向 url 发送请求。如果电子邮件和密码有效,它将显示电子邮件。如果电子邮件等于 HTTP 正文,那么它将重定向到 LoggedOn Storyboard。

发生了什么: Button 将获取电子邮件和密码并向 url 发送请求。如果电子邮件和密码有效,它将显示电子邮件。如果电子邮件等于 HTTP 正文,则它什么也不做。

Storyboard 未连接到 LoggedOn Storyboard。我不知道它是否需要。有人可以帮我弄清楚发生了什么吗?

标签: swift

解决方案


推荐阅读