IOSDev
IOSDev

Reputation: 229

Swift UI + Cannot assign value of type 'JsonResponse' to type 'JsonResponse.Type'

Below is the code : Getting error : Cannot assign value of type 'JsonResponse' to type 'JsonResponse.Type' Not sure why it is showing this error. Any help please

struct JsonResponse: Codable {
    var message: String
    var statusCode: String
    var results: Results
}


    class ReadData: ObservableObject  {
        @Published var response = [Results]()
        @Published var message: String = ""
        @Published var jsonData = JsonResponse.self
    
        init(){
            self.loadData()
        }
        
        func loadData()  {
            guard let url = Bundle.main.url(forResource: "list", withExtension: "json")
                else {
                    print("Json file not found")
                    return
                }
            
            let data = try? Data(contentsOf: url)
            
            if let data = data {
                            if let decodedResponse = try? JSONDecoder().decode(JsonResponse.self, from: data) as JsonResponse {
                                DispatchQueue.main.async {
                                    self.message = decodedResponse.message
                                    self.jsonData = decodedResponse // Cannot assign value of type 'JsonResponse' to type 'JsonResponse.Type'
                                }
                                return
                            }
                        }
        }
         
    }

Upvotes: 0

Views: 442

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100543

Should be

@Published var jsonData:JsonResponse?

Upvotes: 0

Related Questions