Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
481 views
in Technique[技术] by (71.8m points)

swift3 - Converting Swift 2.3 to Swift 3.0 - Error, Cannot invoke 'dataTask' with an argument list of type'

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...

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

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

Any suggestion?

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")
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The compiler wants URLRequest and Error

 ...
 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...