将 URLSession 更新为 swift 3 抛出错误

Posted

技术标签:

【中文标题】将 URLSession 更新为 swift 3 抛出错误【英文标题】:Updating an URLSession to swift 3 throwing error 【发布时间】: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 中是否有不同的方式来执行请求,或者他们只是改变了执行请求的方式?

【问题讨论】:

Converting Swift 2.3 to Swift 3.0 - Error, Cannot invoke 'dataTask' with an argument list of type'的可能重复 【参考方案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


【讨论】:

以上是关于将 URLSession 更新为 swift 3 抛出错误的主要内容,如果未能解决你的问题,请参考以下文章

Swift:UIViewController 实例未从 URLSession 委托方法接收更新

如何在从 Swift 3 中的函数返回结果之前等待 URLSession 完成

Swift 3 URLSession 发送空请求

如何在 Swift 3 中使用带有代理的 URLSession

使用 URLSession 将数据获取到 PHP 方法 - swift

通过证书进行客户端身份验证的 Swift 3 UrlSession