0

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?

Mo Olwan
  • 5
  • 2
  • Do you use `Codable` struct? If not, I'd recommend often to use it, else https://stackoverflow.com/questions/70789753/update-responsejson-to-responsedecodable-in-swift/70804441#70804441 – Larme Apr 20 '22 at 11:48

2 Answers2

1

Just change

AF.request(url).responseJSON to AF.request(url).responseData

SysGreg
  • 21
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 05 '22 at 17:10
0

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)
        }
    }
}
MBT
  • 1,381
  • 1
  • 6
  • 10
  • I am new in swift, for that, i found that i must modify this protocol, how I can edit it?:-> protocol AlamofireSession { func request(url: URL, callback: @escaping (AFDataResponse) -> Void) } – Mo Olwan Apr 20 '22 at 12:13
  • I edited it, Now the error hidden: protocol AlamofireSession { func request(url: URL, callback: @escaping (AFDataResponse) -> Void) } – Mo Olwan Apr 20 '22 at 12:36