Cannot invoke 'decode' with an argument list of type '(GenericDM.Type, from: String?)'

I've been using app.quicktype.io to generate the code to decode JSON. I'm facing a problem now that I have not seen before.

First, I have this simple struct:

import Foundation

struct GenericDM: Codable {
let status, statusMessage: String
let result: [Result]

enum CodingKeys: String, CodingKey {
    case status
    case statusMessage = "status_message"
    case result
}

}

struct Result: Codable {
let applicationID, applicationName, applicationType, suFirstName: String
let suMiddleName, suLastName, suAge, suDob: String
let suRace: String
let suAddress: SuAddress
let createdTime, updatedTime: Int

enum CodingKeys: String, CodingKey {
    case applicationID = "application_id"
    case applicationName = "application_name"
    case applicationType = "application_type"
    case suFirstName = "su_first_name"
    case suMiddleName = "su_middle_name"
    case suLastName = "su_last_name"
    case suAge = "su_age"
    case suDob = "su_dob"
    case suRace = "su_race"
    case suAddress = "su_address"
    case createdTime = "created_time"
    case updatedTime = "updated_time"
}

}

struct SuAddress: Codable {
let addrLine1, addrLine2, stName, addrCity: String
let addrState, addrCounty, addrPin: String

enum CodingKeys: String, CodingKey {
    case addrLine1 = "addr_line_1"
    case addrLine2 = "addr_line_2"
    case stName = "st_name"
    case addrCity = "addr_city"
    case addrState = "addr_state"
    case addrCounty = "addr_county"
    case addrPin = "addr_pin"
}

}

func newJSONDecoder() -> JSONDecoder {
let decoder = JSONDecoder()
if #available(iOS 10.0, OSX 10.12, tvOS 10.0, watchOS 3.0, *) {
decoder.dateDecodingStrategy = .iso8601
}
return decoder
}

func newJSONEncoder() -> JSONEncoder {
let encoder = JSONEncoder()
if #available(iOS 10.0, OSX 10.12, tvOS 10.0, watchOS 3.0, *) {
encoder.dateEncodingStrategy = .iso8601
}
return encoder
}

// MARK: - URLSession response handlers

extension URLSession {
fileprivate func codableTask<T: Codable>(with url: URL, completionHandler: @escaping (T?, URLResponse?, Error?) -> Void) -> URLSessionDataTask {
return self.dataTask(with: url) { data, response, error in
guard let data = data, error == nil else {
completionHandler(nil, response, error)
return
}
completionHandler(try? newJSONDecoder().decode(T.self, from: data), response, nil)
}
}

func genericDMTask(with url: URL, completionHandler: @escaping (GenericDM?, URLResponse?, Error?) -&gt; Void) -&gt; URLSessionDataTask {
    return self.codableTask(with: url, completionHandler: completionHandler)
}

}

Then, I read a JSON file and try to decode the data from a different class:

    let bundle = Bundle.main
let path = bundle.path(forResource: “MockGenericData”, ofType: “json”)
let jsonData = try? String.init(contentsOf: URL.init(fileURLWithPath: path!))
let genericDM = try? newGenericDMJSONDecoder().decode(GenericDM.self, from: jsonData)

I temporarily need to read from that mockup file before I can get it from the backend.

However, I’m getting Cannot invoke ‘decode’ with an argument list of type ‘(GenericDM.Type, from: String?)’ and I don’t understand why.

Anybody has any idea?

#json #swift

8 Likes17.10 GEEK