Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 - How can i define callback function as parameter

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()
}
like image 782
Efe ÖZYER Avatar asked Dec 11 '22 13:12

Efe ÖZYER


2 Answers

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.

like image 162
Lexorus Avatar answered Feb 04 '23 23:02

Lexorus


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..
    }
}
like image 25
Efe ÖZYER Avatar answered Feb 04 '23 23:02

Efe ÖZYER