简体   繁体   中英

Alamofire 4 - Cannot call value of non-function type HTTPURLResponse

I'm updating my app to Swift 3 and I use Alamofire to handle API call to a REST server. This is my function:

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)
    }

I'm getting a Cannot call value of non-function type 'HTTPURLResponse' error at the last return line and I cannot figure out the reason.

You're trying to init a response by using a value instead of a type

response(responseSerializer: responseSerializer, completionHandler: completionHandler)

where actually response is an instantiated constant. The second problem is that the function is supposed to return an Alamofire.Request type, not an HTTPURLResponse type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM