在我的ServerAPI类中,我有这个方法:
func getState(name: String, completionHandler: (result: dto?, error: NSError?) -> Void) {
let parameters = [String: AnyObject]()
/* 1. Make the request */
httpClient.taskForGETMethod(HttpClient.Methods.State, pathParameters: [name], parameters: parameters) { JSONResult, error in
/* 2. Send the desired value(s) to completion handler */
if let error = error {
completionHandler(result: nil, error: error)
} else {
..
completionHandler(result: state, error: nil)
}
}
}
调用我的HttpClient方法:
func taskForGETMethod(method: String, pathParameters: [String], parameters: [String : AnyObject], completionHandler: (result: AnyObject!, error: NSError?) -> Void) -> NSURLSessionDataTask {
..
/* 2. Make the request */
let task = session.dataTaskWithRequest(request) { (data, response, error) in
/* GUARD: Was there an error? */
guard (error == nil) else {
print("There was an error with your request: \(error)")
return
}
...
/* 3. Parse the data and use the data (happens in completion handler) */
HttpClient.parseJSONWithCompletionHandler(data, completionHandler: completionHandler)
}
/* 4. Start the request */
task.resume()
return task
}
您能否解释一下HttpClient
如何返回NSURLSessionDataTask
,而API method
调用除外{ JSONResult, error in
?
答案 0 :(得分:0)
听起来你将最后一个参数(完成处理程序)与返回值(数据任务)混淆。抛弃返回值,否则将读取let task = httpClient.taskForGETMethod...