Learn Courses My Dashboard

Having issue getting image data from network call

I’m trying to display an image in my ContentView from an API. However, I am getting an error of my “CharacterResults” class. It is giving me "CharacterResults does not conform to protocol 'Decodable and ‘Encodable’ ". I have tried to update the Character struct to a class but that provides the same error.

Model:


struct Character: Codable {
    
    var results: [CharacterResults]
}




class CharacterResults: Codable, Identifiable, ObservableObject {
    
    @Published var imageData: Data?
    
    let id = UUID()
    var name: String?
    var status: String?
    var species: String?
    var origin: Origin?
    var location: Location?
    var image: String?
    
    
    func getImageData(){
        guard image != nil else {
            return
        }

        if let imageURL = URL(string: image!) {
            let dataTask = URLSession.shared.dataTask(with: imageURL) { data, response, error in
                if error == nil {
                    DispatchQueue.main.async {
                        self.imageData = data!
                    }
                }
            }
            dataTask.resume()
        }
    }

}

struct Origin: Codable {
    var name: String
}

struct Location: Codable {
    var name: String
}