I am trying to create a callback on swift 3 but haven't had any luck so far. I was taking a look at this question: link which is similar, but the answer gives me an error.
Basically I have an API struct with a static function that I need to have a 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" do { 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 as URLRequest, completionHandler: { data, response, error in guard error == nil else { return } guard let data = data else { return } DispatchQueue.main.async { do { 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 to call callback gives me an error: extra 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) { do { let jsonError = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] let message = jsonError?["message"] as! String } catch { } } } } }) task.resume() } } So I basically can't call the callback success because it gives me an error: Extra argument in call. Any idea on how to fix it?
My goal is to call:
API.functionWithCallback(params: params, success() -> Ticket { //do something with the returned ticket here }, error() -> () { //do something with the error message here } )