将Swift 2.3转换为Swift 3.0-错误,无法使用类型为实参的列表调用'dataTask' [英] Converting Swift 2.3 to Swift 3.0 - Error, Cannot invoke 'dataTask' with an argument list of type'

查看:167
本文介绍了将Swift 2.3转换为Swift 3.0-错误,无法使用类型为实参的列表调用'dataTask'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的项目之一从Swift 2.3转换为Swift 3.0,但是由于某些原因,我收到以下错误消息...

I'm trying to convert one of my projects from Swift 2.3 to Swift 3.0 but some reason I get the following error...

无法使用类型为'的参数列表调用'dataTask'(with:NSMutableURLRequest,completeHandler:(Data ?, UIRLResponse ?, NSError)-> Void)'

Cannot invoke 'dataTask' with an argument list of type'(with: NSMutableURLRequest, completionHandler:(Data?, UIRLResponse?, NSError) -> Void)'

使用以下部分匹配的参数列表存在"dataTask"的重载:(带有:URLRequest,completionHandler:@escaping(Data?,URLResponse ?、错误?)->无效),(带有:URL,completionHandler:@escaping(数据?,URLResponse ?、错误?)->无效)

Overloads for ‘dataTask’ exist with these partially matching parameter lists: (with: URLRequest, completionHandler:@escaping(Data?, URLResponse?, Error?) -> Void), (with: URL, completionHandler: @escaping(Data?, URLResponse?, Error?) -> Void)

下面的代码在此行中显示错误...

The error shows in this line from the code below...

let task = session.dataTask(with: request, completionHandler: { (responseData:Data?, response:URLResponse?, error:NSError?) -> Void in

有什么建议吗?

这里是发生错误的整个函数代码.

Here is the whole code for function where the error occurs.

func verifyReceipt(_ transaction:SKPaymentTransaction?){
        let receiptURL = Bundle.main.appStoreReceiptURL!
        if let receipt = try? Data(contentsOf: receiptURL){
            //Receipt exists
            let requestContents = ["receipt-data" : receipt.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))]

            //Perform request
            do {
                let requestData = try JSONSerialization.data(withJSONObject: requestContents, options: JSONSerialization.WritingOptions(rawValue: 0))

                //Build URL Request
                let storeURL = URL(string: "https://buy.itunes.apple.com/verifyReceipt")// production URL
                //let storeURL = NSURL(string: "https:/sandbox.itunes.apple.com/verifyReceipt") // Testing URL
                let request = NSMutableURLRequest(url: storeURL!)
                request.httpMethod = "Post"
                request.httpBody = requestData

                let session = URLSession.shared
                let task = session.dataTask(with: request, completionHandler: { (responseData:Data?, response:URLResponse?, error:NSError?) -> Void in
                    //

                    do {
                        let json = try JSONSerialization.jsonObject(with: responseData!, options: .mutableLeaves) as! NSDictionary

                        print(json)

                        if (json.object(forKey: "status") as! NSNumber) == 0 {
                            //

                            if let latest_receipt = json["latest_receipt_info"]{
                                self.validatePurchaseArray(latest_receipt as! NSArray)
                            } else {
                                let receipt_dict = json["receipt"] as! NSDictionary
                                if let purchases = receipt_dict["in_app"] as? NSArray{
                                    self.validatePurchaseArray(purchases)
                                }
                            }

                            if transaction != nil {
                                SKPaymentQueue.default().finishTransaction(transaction!)
                            }

                            DispatchQueue.main.sync(execute: { () -> Void in
                                self.delegate?.managerDidRestorePurchases()
                            })


                        } else {
                            //Debug the receipt
                            print(json.object(forKey: "status") as! NSNumber)
                        }

                    } catch {
                        print(error)
                    }
                })

                task.resume()

            } catch {
                print(error)
            }

        } else {
            //Receipt does not exist
            print("No Receipt")
        }
    }

推荐答案

编译器需要URLRequestError

 ...
 var request = URLRequest(url: storeURL!)
 request.httpMethod = "Post"
 ...
 let task = session.dataTask(with: request, 
                completionHandler: { (responseData:Data?, 
                                          response:URLResponse?, 
                                             error:Error?) -> Void in

我建议省略所有类型注释

I recommend to omit all type annotations

 let task = session.dataTask(with: request, 
                completionHandler: { (responseData, response, error) -> Void in

这篇关于将Swift 2.3转换为Swift 3.0-错误,无法使用类型为实参的列表调用'dataTask'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆