怎么回事`HttpClient`返回`NSURLSessionDataTask`但是`API方法`调用excepts` {JSONResult,错误in`

时间:2016-02-02 20:09:58

标签: ios xcode swift http

在我的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

1 个答案:

答案 0 :(得分:0)

听起来你将最后一个参数(完成处理程序)与返回值(数据任务)混淆。抛弃返回值,否则将读取let task = httpClient.taskForGETMethod...