Swift 3 - How can i define callback function as parameter

15,952

Solution 1

Thanks for reply but finally solution

class HttpRequest<Request, Response>
{
    private var serviceBase:String = "http://192.168.1.1/api";
    func request(path:String, model: Request, success: @escaping((_ response: [Response]?) -> ()), failure: @escaping ((_ error:String) -> ()) {
         // code here..
    }
}

Solution 2

You can just pass the closure(function) as parameter

swift
func request(requestPath:String, requestParams:Any, callback:((Result) -> Void)) { 
    ... 
}

Where Result will be the type of your response.

Share:
15,952
Efe ÖZYER
Author by

Efe ÖZYER

"Code" is good Mobile, Web, Desktop

Updated on June 13, 2022

Comments

  • Efe ÖZYER
    Efe ÖZYER about 2 years

    want to create global http request function/extension with Alamofire it's like

    function Request(requestPath:String, requestParams:Any, onComplate:Void) {
     // stuff here, when async request complate i want to call onComplate function
     // like C# method.Invoke() or func.Invoke()
    }
    
  • Efe ÖZYER
    Efe ÖZYER over 7 years
    Thanks for reply check my solution :)