Home > Enterprise >  Swift UI Cannot assign value of type 'JsonResponse' to type 'JsonResponse.Type'
Swift UI Cannot assign value of type 'JsonResponse' to type 'JsonResponse.Type'

Time:02-07

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
                            }
                        }
        }
         
    }

CodePudding user response:

Should be

@Published var jsonData:JsonResponse?
  •  Tags:  
  • Related