Swift pass in struct as function parameter

Viewed 3211

I have a JSON parsing class like so

class JSONParser: NSObject {
    let newJSONDecoder : JSONDecoder
    let data : Data

    init(decoder: JSONDecoder, data: Data, model:  ) {
        self.newJSONDecoder = JSONDecoder()
        self.data = data
    }
}

The goal is to have the model parameter be a class that can take in any data and any model and create an object and return it to the calling class instance. EG below

let jsonParser = JSONParser(myDecoder, data, struct)
let parsedArray = jsonParser.createJSONArray()

Can I pass in a struct to the JSONParser init method of type struct and not of type struct "class" name (eg ModelStruct)?

Eventually, the struct parameter should get used in this function

try newJSONDecoder.decode(model.self, from:data!), so the second issue is how to get it into that function - won't work if printed as a String.

2 Answers

You can do something like below:

class JSONParser: NSObject {
    let newJSONDecoder : JSONDecoder
    let data : Data

    init<T: Codable>(data: Data, model: T.Type) {
        self.newJSONDecoder = JSONDecoder()
        do {
        let result = try self.newJSONDecoder.decode(model.self, from: data)
        print(result)
        } catch let err {
            print(err.localizedDescription)
        }
        self.data = data
    }
}

Your model struct:

struct TestModel: Codable {
    let name: String
    let age: Int
}

How you call init:

let str = """
        {"name": "Robert", "age" : 35}
        """

    let data = str.data(using: .utf8)
    let jsonParser = JSONParser(data: data!, model: TestModel.self)

Here is how I pass generic Codable struct as a parameter and also return it via completion handler as parameter, allGeneric :

func getAccordingToWebServiceFlag<T:Decodable>(flagSender: WebServicesFlagSenders,codableStruct: T.Type ,completionHandler: @escaping ( _ publicDataResponseModel:T?,_ flagSender: WebServicesFlagSenders) -> Void) {

excuteServerOperation(nil, imageData: nil, url:ServerAPIServant.webServiceFullURL(webServicesFlagSenders: flagSender), way: .get, flagSender: flagSender,completionHandler: { (result, flagSender) in
    AppDelegate().printStringBy_ispha(string: "  \(flagSender) Hmmm  \(result)")
    do {
        let jsonData = try JSONSerialization.data(withJSONObject:  result , options: .prettyPrinted)
        let decodableResponse = try! JSONDecoder().decode(codableStruct, from: jsonData)

        HelpingMethods.printCustomObjectBy_ispha(anyObject: decodableResponse as AnyObject)
        completionHandler(decodableResponse,flagSender)
    } catch let error {
        HelpingMethods.printStringBy_ispha(string: " Codable failure with error = \(error.localizedDescription)")
         completionHandler(nil,flagSender)
    }


}
)}
Related