There is the following code, but it does not work as expected because Swift gives errors.
import UIKit enum ObjectStatus: String, Codable { case Arm = "Arm" case Disarm = "Disarm" } struct Testobject: Codable { var id: Int var name: String? var city: String var status: ObjectStatus //var active: Bool } func decodejson<T>(_ value:String) -> T? { let jsonDecoder = JSONDecoder() let decode = try jsonDecoder.decode(T.self as! Codable, from: input_json.data(using: String.Encoding.utf8)!) dump(decode) } let input_json = "{\"status\": \"Arm\", \"id\" : 1,\"name\" : null,\"city\" : \"Moscow\", \"active\": true}" if let my_object = decodejson<Testobject>(input_json) { dump(my_object) }
I am trying to write one decoding function and pass json and the type of target object into which json should be decoded. That would not write for each object its decoding function