我正在将我的应用更新为Swift 3,并使用Alamofire处理对REST服务器的API调用。这是我的功能:
extension Alamofire.Request {
func responseAllCareers(_ username: String, password: String, completionHandler: @escaping (DataResponse<Careers>) -> Void) -> Self {
let responseSerializer = DataResponseSerializer<Careers> { request, response, data, error in
guard error == nil else {
return .failure(BackendError.network(error: error!))
}
let JSONResponseSerializer = DataRequest.jsonResponseSerializer(options: .allowFragments)
let result = JSONResponseSerializer.serializeResponse(request, response, data, error)
guard case let .success(jsonObject) = result else {
return .failure(BackendError.jsonSerialization(error: result.error!))
}
switch result {
case .success(let value):
let api: APIMessage = APIMessage(json: value as! JSON)!
var careers: Careers = Careers()
// check for correct credentials
if api.result != "failure" {
careers = Careers(json: value as! JSON)
CacheManager.sharedInstance.storeCredentials(username, password: password)
CacheManager.sharedInstance.storeJsonInCacheByKey(CacheManager.CAREERS, json: value as! JSON)
careers.areCredentialsValid = true
}
return .success(careers)
case .failure(let error):
return .failure(error)
}
}
return response(responseSerializer: responseSerializer, completionHandler: completionHandler)
}
我收到无法在最后一个返回行调用非函数类型'HTTPURLResponse'错误的值,我无法弄清楚原因。
答案 0 :(得分:0)
您尝试使用值而不是类型
来初始化响应response(responseSerializer: responseSerializer, completionHandler: completionHandler)
其中实际response
是实例化的常量。
第二个问题是该函数应该返回Alamofire.Request
类型,而不是HTTPURLResponse
类型。