【问题标题】:Updating an URLSession to swift 3 throwing error将 URLSession 更新为 swift 3 抛出错误
【发布时间】:2016-11-21 17:15:21
【问题描述】:

我将我的代码更新为 Swift 3,除了 URLSession 之外,大部分代码都转换得很好,我找不到解决这个错误的方法:

无法使用类型为“(带有:NSMutableURLRequest,completionHandler:(Data?,URLResponse?,NSError?)-> Void)”类型的参数列表调用“dataTask”

这是我的代码:

    let post:NSString = "username=\(username)&userPassword=\(password)&userEmail=\(email)" as NSString

    let url:URL = URL(string: "http://ec2-54-201-55-114.us-west-2.compute.amazonaws.com/wickizerApp/ApplicationDB/scripts/registerUser.php")!

    let request = NSMutableURLRequest(url: url)
    request.httpMethod = "POST"
    request.httpBody = post.data(using: String.Encoding.utf8.rawValue)


    URLSession.shared.dataTask(with: request, completionHandler: { (data:Data?, response:URLResponse?, error:NSError?) -> Void in

        DispatchQueue.main.async
            {

                if error != nil {
                    self.displayAlertMessage(error!.localizedDescription)
                    return
                }

                do {
                    let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary

                    if let parseJSON = json {

                        let status = parseJSON["status"] as? String

                        if( status! == "200")
                        {
                            let myAlert = UIAlertController(title: "Alert", message: "Registration successful", preferredStyle: UIAlertControllerStyle.alert);

                            let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default){(action) in

                                self.dismiss(animated: true, completion: nil)
                            }

                            myAlert.addAction(okAction);
                            self.present(myAlert, animated: true, completion: nil)
                        } else {
                            let errorMessage = parseJSON["message"] as? String
                            if(errorMessage != nil)
                            {
                                self.displayAlertMessage(errorMessage!)
                            }

                        }

                    }
                } catch{
                    print(error)
                }



        }

    }).resume()

在 swift 3 中是否有不同的方式来执行请求,或者他们只是改变了执行请求的方式?

【问题讨论】:

标签: swift3


【解决方案1】:

编译器需要URLRequestError

var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = post.data(using: .utf8)

URLSession.shared.dataTask(with: request, completionHandler: { (data:Data?, response:URLResponse?, error:Error?) -> Void in

})

或者更短

URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in

})

或者更短

URLSession.shared.dataTask(with: request) { (data, response, error) in

}

【讨论】:

    猜你喜欢
    • 2017-06-22
    • 1970-01-01
    • 2017-03-21
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    • 2015-03-27
    相关资源
    最近更新 更多