Home > database >  responseJSON deprecated and will be removed in Alamofire 6. Use responseDecodable instead
responseJSON deprecated and will be removed in Alamofire 6. Use responseDecodable instead

Time:04-20

In swift, when i use the following code:

final class EdamamSession: AlamofireSession {
    func request(url: URL, callback: @escaping (AFDataResponse<Any>) -> Void) {
        AF.request(url).responseJSON { dataResponse in
            callback(dataResponse)
        }
    }
}

I get this warning : 'responseJSON(queue:dataPreprocessor:emptyResponseCodes:emptyRequestMethods:options:completionHandler:)' is deprecated: responseJSON deprecated and will be removed in Alamofire 6. Use responseDecodable instead.

Then when I replace "responseJSON" with "responseDecodable", then I get this error "Protocol 'Any' as a type cannot conform to 'Decodable'".

Any solution Please?

CodePudding user response:

Try this. I am using Alamofire 5.5.0

final class EdamamSession: Session {
    func request<T: Decodable>(url: URL, callback: @escaping (DataResponse<T, AFError>) -> Void) {
        AF.request(url).responseDecodable { (response: DataResponse<T, AFError>) in
            callback(response)
        }
    }
}
  • Related