我想用 Alamofire 创建一个全局 HTTP 请求函数/扩展。就像:
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()
}
您可以将闭包(函数)作为参数传递
swift
func request(requestPath:String, requestParams:Any, callback:((Result) -> Void)) {
...
}
其中
Result
将是您的回复类型。
感谢回复,终于解决了
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..
}
}