将Swift 2.3转换为Swift 3.0 - 错误,无法调用' dataTask'使用类型'

时间:2016-11-06 16:31:05

标签: swift swift3

我试图将我的一个项目从Swift 2.3转换为Swift 3.0,但有些原因我得到以下错误......

  
    

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

         

'dataTask'的重载与这些部分匹配的参数列表一起存在:( with:URLRequest,completionHandler:@escaping(Data?,URLResponse?,Error?) - > Void),(with:URL,completionHandler:@escaping) (数据?,URLResponse?,错误?) - > Void)

  

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

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

有什么建议吗?

以下是发生错误的函数的完整代码。

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")
        }
    }

2 个答案:

答案 0 :(得分:7)

编译器需要URLRequestError

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

我建议省略所有类型注释

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

答案 1 :(得分:0)

var request = URLRequest(url: storeURL!)
 request.httpMethod = "Post"

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

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

https://swift.org/migration-guide-swift3/