2016-06-07 38 views
0

我有我的APIServices類中定義該功能如何繼承與關閉功能在斯威夫特

typealias APIResponseOK = (data:NSDictionary, extra:NSDictionary) -> Void 
typealias APIResponseError = (failure:Bool, code:NSString, message:NSString) -> Void 

func getHttp(action:NSString, onResult:APIResponseOK, onError:APIResponseError) -> Void { 

    let strUrlRequst = String(format: "\(action)") 

    Alamofire.request(.GET, strUrlRequst).responseJSON { (responseData) -> Void in 

     if(responseData.result.error == nil){ 

      if((responseData.result.value) != nil) { 
       let swiftyJsonVar = JSON(responseData.result.value!) 

       print("Response \(swiftyJsonVar)") 

       onResult(data: swiftyJsonVar.dictionaryObject!, extra: swiftyJsonVar.dictionaryObject!); 
      } 
     } 
     else{ 
      onError(failure: true, code: "OM_ERR", message: (responseData.result.error?.localizedDescription)!); 
     } 

    } 
} 

現在我要繼承的ViewController類此功能。我試過的是如下。

apiServices.getHttp("Somename", onResult: (data:NSDictionary, extra:NSDictionary){ 

     }, 
    onError:(failure: Bool, code:NSString, message:NSString){ 


    }) 

爲什麼我得到這個錯誤。請糾正我,我非常新的,以迅速

enter image description here

+2

你看過如何在Swift中編寫閉包嗎?你使用的語法相當遙遠。 – luk2302

+0

你是如何宣佈APIResponseOK關閉的? –

+0

請看我如何申報APIResponseOK。我已經用聲明更新了源代碼 – sajaz

回答

0

糾正你的函數聲明:

func getHttp(action:NSString, onResult:APIResponseOK, onError:APIResponseError) -> Void { 

有:

func getHttp(action:NSString, onResult:APIResponseOK, onError:APIResponseError) { 

之後,調用此功能,您可以做:

let myApiSuccess: APIResponseOK = {(data:NSDictionary?, extra:NSDictionary?) -> Void in 
    print ("Api Success : result is:\n \(data)") 
    // Here you can make whatever you want with your dictionaries 
} 

let myApiFailure: APIResponseError = {(failure:Bool?, code:NSString?, message:NSString?) -> Void in 
    print ("Api Failure : error is:\n \(message)") 
    // Here you can check the errors with your vars looking for failure, code and message 
} 

getHttp(action:NSString, onResult:APIResponseOK, onError:APIResponseError) 

可以在this SO answer

1
apiServices.getHttp("Somename", onResult:{ data: NSDictionary, extra: NSDictionary in 
    // some 
}, onError:{ failure: Bool, code: NSString, message: NSString in 
    // some 
}) 

找到更多的細節您應該檢查所有的蘋果斯威夫特文檔,而不是首先使用它。 和其他問題一樣,爲什麼你要在Swift中使用NSString或NSDictionary。