Error creating REST request with Swift to Google app engine endpoint

对着背影说爱祢 提交于 2020-01-03 03:15:25

问题


I am try to make a request for my object using Swift for Google app engine endpoint, but am getting an error and I have tried to correct it but it won't just get passed. I am new to iOS development anyway.

import UIKit

class MainController: UITabBarController {
    var service : GTLRMyApiService?
    var myvalue: String?

    override func viewDidLoad() {

        if service == nil {
            service = GTLRMyApiService()
            service?.retryEnabled = true

        }

        let query : GTLRMyApiQuery_GetRates = GTLRMyApiQuery_GetRates.query()

        var ticket = GTLRServiceTicket.initialize
        ticket = service!.executeQuery(query, completionHandler: { (ticket: GTLRServiceTicket,  object: AnyObject!, error: NSError) -> Void in
            print("Analytics: \(object) or \(error)")
            let resp : GTLRMyApi_MyRates = object as! GTLRMyApi_MyRates
            print("Black Market Dollar rate is: \(resp.value1)")
            self.myvalue = resp.value1

    })

}

It gives the error:

Cannot convert value of type '(GTLRServiceTicket, AnyObject!, NSError) -> Void' to expected argument type 'GTLRServiceCompletionHandler?'


回答1:


I had this same problem and solved it by changing the unwrapping of the parameters in the closure:

ticket = service!.executeQuery(query, completionHandler: { (ticket: GTLRServiceTicket!,  object: AnyObject?, error: NSError?) -> Void in
        print("Analytics: \(object) or \(error)")
        let resp : GTLRMyApi_MyRates = object as! GTLRMyApi_MyRates
        print("Black Market Dollar rate is: \(resp.value1)")
        self.myvalue = resp.value1

})

I'm no Swift expert, but I guess this is required to match what the Objective-C implementation of the function is expecting. Xcode does not make this clear.




回答2:


I too faced same issue but by removing unneeded optionals it will works fine.

  self.service.executeQuery(batchQuery) { (ticket:GTLRServiceTicket, object:Any?, error:Error?) in
        let data: GTLRBatchResult = object as! GTLRBatchResult
        var messages = data.successes
        for datata in messages!{

            var mussage : GTLRGmail_Message = datata.value as! GTLRGmail_Message
            print(mussage.identifier)

        }
    }


来源:https://stackoverflow.com/questions/38199860/error-creating-rest-request-with-swift-to-google-app-engine-endpoint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!