我在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){
})
为什么我收到此错误。请纠正我,我非常喜欢快速
答案 0 :(得分:1)
apiServices.getHttp("Somename", onResult:{ data: NSDictionary, extra: NSDictionary in
// some
}, onError:{ failure: Bool, code: NSString, message: NSString in
// some
})
您应该检查所有Apple Swift文档,而不是先使用它。 还有其他问题,比如,你为什么要在Swift中使用NSString或NSDictionary。
答案 1 :(得分: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
中找到更多详情