获取网络请求后无法在 IBAction 中执行任何代码

Cannot Perform Any Code in IBAction after getting network request

在确认我从 Udacity API 获得会话 ID 后,我正在尝试过渡到一个新的 segue。但是,此后我无法获得 运行 的任何代码。会话 ID 和所有信息将打印出来,但之后什么都不会完成。我在函数中做错了什么吗?我尝试执行 UIUpdates 然后尝试打印 "Hello" 但这也没有用。下面是一些代码以及我的 GitHub 个人资料。 https://github.com/SteveBurgos95/UdacityMapProject

    @IBAction func loginButton(_ sender: Any) {
UdacityClient.sharedInstance().loginWithUsername(emailTextField.text!, password: passwordTextField.text!){ (success, error) in
    performUIUpdatesOnMain {
        if success {
            print("Hello")
        } else {
            print("Hey")
        }
    }
}

private func completeLogin() {
emailTextField.text = ""
let controller = storyboard!.instantiateViewController(withIdentifier: "TableMapViewController") as! UINavigationController
present(controller, animated: true, completion: nil)

在我看来,当 dataTask 完成时(在错误和成功的情况下),您从未在 UdacityClient.swift 中调用完成处理程序?

这应该是这样的:

let task = session.dataTask(with: request as URLRequest) { data, response, error in

    // Make sure there is no error, or else
    guard error == nil else { // Handle error…

      completionHandler(false, error?.localizedDescription)
      /*
         func sendError(_ error: String) {
         print(error)
         let userInfo = [NSLocalizedDescriptionKey : error]
         completionHandlerForGET(nil, NSError(domain: "taskForGETMethod", code: 1, userInfo: userInfo))
        */
        return
    }

    completionHandler(true, nil)
}

像下面这样更改函数

     func loginWithUsername(_ username: String, password: String, completionHandler: @escaping (_ success: Bool, _ errorMessage: String?) -> Void ) {
        let request = NSMutableURLRequest(url: URL(string: "https://www.udacity.com/api/session")!)
        request.httpMethod = "POST"
        request.addValue("application/json", forHTTPHeaderField: "Accept")
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")

        request.httpBody = "{\"udacity\": {\"username\": \"\(username)\", \"password\": \"\(password)\"}}".data(using: String.Encoding.utf8)

        let session = URLSession.shared
        let task = session.dataTask(with: request as URLRequest) { data, response, error in
            if error != nil 
                // Handle error…
              /*
                 func sendError(_ error: String) {
                 print(error)
                 let userInfo = [NSLocalizedDescriptionKey : error]
                 completionHandlerForGET(nil, NSError(domain: "taskForGETMethod", code: 1, userInfo: userInfo))
                */

                return
            }
           completionHandler(true,nil)
            let range = Range(5..<data!.count)
            let newData = data?.subdata(in: range) /* subset response data! */
            print(NSString(data: newData!, encoding: String.Encoding.utf8.rawValue)!)
        }
        task.resume()
        }

查看代码后,UI 未更新,因为您尚未在 UdacityClient.swift 中实现 sucess 块。 sucessblock/closureescapes。所以您不会在

收到回调

UdacityClient.sharedInstance().loginWithUsername(emailTextField.text!, password: passwordTextField.text!){