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

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.

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

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

What is JSON - JSON Parsing in Swift

What is JSON and how do you parse JSON in Swift? JSON stands for JavaScript Object Notation and it's a way to represent data in a standard way. Learn how to parse JSON in Swift using the Codable protocol. We take the response JSON data from a real API call and then parse it in Swift!

Hire Dedicated Swift Developers

Want to create a native iOS application for your Startup? **[Hire Dedicated Swift Developers](https://hourlydeveloper.io/hire-dedicated-swift-developer/ "Hire Dedicated Swift Developers")** for end-to-end services like development, migration...

JSON Parsing in Android - Step by Step Implementation

JSON Parsing in Android - What is JSON, Comparison betwen JSON and XML, JSON parsing step by step implementation and functions.

# Google's Flutter Tutorial- Easily Parse Complex JSON, Create JSON Model Classes.

Welcome to Flutter tutorial This demo helps you to easily parse any complex JSON data in to Dart Model classe easily. Proper Error Handling in Flutter https:...

How to change the UITextfield text with data from JSON based on the value from UISlider in Swift 4?

Right now I have the UIText field hard coded and changing according to the value on the UISlider.