2016-06-07 18 views
0

의 클로저 기능을 상속 어떻게스위프트

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 클래스에서이 함수를 정의해야합니다. 제가 시도한 것은 노호처럼입니다.

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

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


    }) 

왜이 오류가 발생합니까? 당신이 할 수있는이 함수를 호출하는, 그 후

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

:

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

과 : 제발 올바른, 당신의 함수 선언을 수정 매우 새로운 신속

enter image description here

+2

Swift에서 클로저를 작성하는 방법을 살펴 보셨습니까? 당신이 사용하는 문법은 아주 멀리 떨어져 있습니다. – luk2302

+0

APIResponseOK 종결을 어떻게 선언하셨습니까 ???? –

+0

APIResponseOK 선언 방법을 참조하십시오. 선언을 사용하여 소스 코드를 업데이트했습니다. – sajaz

답변

0

에 대한 생각 해야 할 일 :

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를 사용하려는 이유와 같은 다른 문제가 있습니다.