2016-12-11 83 views
4

要創建全球http請求功能/擴展與Alamofire它就像斯威夫特3 - 如何定義回調函數的參數

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() 
} 
+0

可以使用Clousers –

回答

3

感謝您的答覆,但最終的解決方案

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.. 
    } 
} 
5

你可以只通過關閉(功能)作爲參數

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

其中Result將是您回覆的類型。

+0

感謝您的回覆檢查我的解決方案是最好的選擇:) –