swift3 - Callback syntax in swift 3 -
i trying create callback on swift 3 haven't had luck far. taking @ question: link similar, answer gives me error.
basically have api struct static function need have callback.
import uikit struct api { public static func functionwithcallback(params: dictionary<string, string>, success: @escaping ((_ response: string) -> ticket), failure: @escaping((_ error:string) -> string) ) { let app_server_url = "http://api.com" + params["key"]! let url: url = url(string: app_server_url)! var request: urlrequest = urlrequest(url: url) request.httpmethod = "post" { request.httpbody = try jsonserialization.data(withjsonobject: params, options: .prettyprinted) } catch let error { print(error.localizeddescription) } request.addvalue("application/json charset=utf-8", forhttpheaderfield: "content-type") request.addvalue("application/json charset=utf-8", forhttpheaderfield: "accept") let session = urlsession.shared let task = session.datatask(with: request urlrequest, completionhandler: { data, response, error in guard error == nil else { return } guard let data = data else { return } dispatchqueue.main.async { { let json = try jsonserialization.jsonobject(with: data) as! [string: any] print(json) var message = "" if let result = json["result"] as? string { if(result == "success") { //attempt call callback gives me error: argument in call success("") { let ticket = json["ticket"] as! nsdictionary var date = ticket["date"] as! string var ticket: ticket = nil ticket.setdate(date: date) return ticket } } else { message = json["message"] as! string print(message) } } catch let error { print(error.localizeddescription) let description = error.localizeddescription if let data = description.data(using: .utf8) { { let jsonerror = try jsonserialization.jsonobject(with: data, options: []) as? [string: any] let message = jsonerror?["message"] as! string } catch { } } } } }) task.resume() } }
so can't call callback success because gives me error: argument in call. idea on how fix it?
my goal call:
api.functionwithcallback(params: params, success() -> ticket { //do returned ticket here }, error() -> () { //do error message here } )
i believe have wrong on how use call closures, can understand of question want ticket in call closure , should parameter of closure not return type of closure.
replace function declaration this:
public static func functionwithcallback(params: dictionary<string, string>, success: @escaping ((_ response: string, _ ticket: ticket) -> void), failure: @escaping((_ error:string) -> void) ) {
and inside function replace this:
success("") { let ticket = json["ticket"] as! nsdictionary var date = ticket["date"] as! string var ticket: ticket = nil // im not sure trying line give error ticket.setdate(date: date) return ticket }
with:
let ticket = json["ticket"] as! nsdictionary var date = ticket["date"] as! string var ticket: ticket = nil // fix line ticket.setdate(date: date) success("",ticket)
and can call function this:
api.functionwithcallback(params: params, success: { response, ticket in // can use ticket here // , response text }) { errormessage in // use error message here }
Comments
Post a Comment