Swift 3 - How to define a callback function as a parameter

want to create a global function / extension of an HTTP request using Alamofire as

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()
}
+4
source share
3 answers

Thanks for the answer, but finally the 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..
    }
}
+3
source

You can just pass the closure (function) as a parameter

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

Where Resultwill be the type of your answer.

+5
source

Source: https://habr.com/ru/post/1663470/


All Articles