I am have been trying to use the video for the Youtube API to create an API for GIPHY. I am having issues getting the data to compile. Can someone please help? See code below:
Error: Response could not be decoded because of error:
The data couldn’t be read because it isn’t in the correct format.
import Foundation
import Alamofire
class GIF_Model: ObservableObject {
@Published var GIFs = [GIF]()
init() {
getGIFs()
}
func getGIFs() {
// Create a url object
guard let url = URL(string: "https://api.giphy.com/v1/gifs/trending") else {
return
}
// Get a decoder
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
// Create a URL request
AF.request(
url
, parameters: ["api_key": Constants.API_KEY, "limit": "10", "rating": "pg"]
)
.validate()
.responseDecodable(of: Response.self, decoder: decoder) { response in
// Check that the call was successful
switch response.result {
// If a success, dont do anything
case .success:
break
// If a failure, log the description and dont do anything else
case .failure(let error):
print(error.localizedDescription)
return
}
// Update the UI with the videos
if let data = response.value?.data {
// Must use main thread because it updates the UI
DispatchQueue.main.async {
self.GIFs = data
}
}
}
}
}