Learn Courses My Dashboard

GIPHY API Help - AF.request Not Compiling

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() {

func getGIFs() {
    // Create a url object
    guard let url = URL(string: "https://api.giphy.com/v1/gifs/trending") else {

    // Get a decoder
    let decoder = JSONDecoder()
    decoder.dateDecodingStrategy = .iso8601
    // Create a URL request
        , parameters: ["api_key": Constants.API_KEY, "limit": "10", "rating": "pg"]
    .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:
            // If a failure, log the description and dont do anything else
        case .failure(let error):
        // 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


Your data model isn’t in the correct format.
What’s the data look like from the API vs the model you wrote in Swift?

{“data”:[{“type”:“gif”,“id”:“e5ZX87yxX2hcidkWi3”,“url”:“https://giphy.com/gifs/nba-e5ZX87yxX2hcidkWi3",“slug”:“nba-e5ZX87yxX2hcidkWi3”,“bitly_gif_url”:“https://gph.is/g/aK3Gbz8”,“bitly_url”:“https://gph.is/g/aK3Gbz8”,“embed_url”:“https://giphy.com/embed/e5ZX87yxX2hcidkWi3”,“username”:“nba”,“source”:"",“title”:"Nba Playoffs Sport GIF by NBA”,“rating”:“g”,“content_url”:"",“source_tld”:"",“source_post_url”:"",“is_sticker”:0,“import_datetime”:“2022-04-25 02:27:05”,“trending_datetime”:“2022-04-25 03:14:53”,“images”:{“original”:

import Foundation

struct GIF: Decodable {

var id = ""
var url = ""
var title = ""
var username = ""
var source = ""
var rating = ""
var import_datetime = Date()
var profile_url = ""
var display_name = ""

enum CodingKeys: String, CodingKey {
    case id
    case url
    case title
    case username
    case source
    case rating
    case import_datetime
    case user
    case profile_url
    case display_name

// default init for previews
init() {
    self.id = "123456789"
    self.url = "http://giphy.com/gifs/confused-flying-YsTs5ltWtEhnq"
    self.title = "Title of GIF"
    self.username = "Username that created GIF"
    self.source = "Source of GIF"
    self.rating = "rated: g"
    self.import_datetime = Date()
    self.profile_url = "profile_url of who made GIF"
    self.display_name = "display_name of User that created GIF"

init(from decoder: Decoder) throws {
    // Parse from the data container
    let container = try decoder.container(keyedBy: CodingKeys.self)
    self.id = try container.decode(String.self, forKey: .id)
    self.url = try container.decode(String.self, forKey: .url)
    self.title = try container.decode(String.self, forKey: .title)
    self.username = try container.decode(String.self, forKey: .username)
    self.source = try container.decode(String.self, forKey: .source)
    self.rating = try container.decode(String.self, forKey: .rating)
    self.import_datetime = try container.decode(Date.self, forKey: .import_datetime)
    // Drill into the user container to extract profile_url, and display_name
    let userContainer = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .user)
    self.profile_url = try userContainer.decode(String.self, forKey: .profile_url)
    self.display_name = try userContainer.decode(String.self, forKey: .display_name)


I ended up removing the following variables, which were kind of extra information, and it worked! I was looking in the wrong place for the solution. Thank you for the help!

var rating = “”
var import_datetime = Date()
var profile_url = “”
var display_name = “”