Alamofire: responseJSON deprecated how to fix on my code

36 Views Asked by At

I am getting following warning on Xcode using Alamofire

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

Here is the code: this code is working but give above warning.

func videoDuration(_ videoId: String, completionHandler: @escaping (String?, Error?) -> ()) {
    
    let URL = "https://www.googleapis.com/youtube/v3/videos?id=\(videoId)&part=contentDetails&key=\(apiKey)"
    var duration = String()
    AF.request(URL).responseJSON { response in
        if let result = response.value as? [String : Any],
            let main = result["items"] as? [[String : Any]]{
            for obj in main{
                duration = (obj as NSDictionary).value(forKeyPath:"contentDetails.duration") as! String
                completionHandler(duration, nil)
                
            }
        }
    }
}

Any help with responseDecodable for the above will be greatly appreciated

0

There are 0 best solutions below