I want to create variable with type of Codable. And later to use it in the JSONEncoder class. I thought that code from below should work fine, but it gives me error:
Cannot invoke
encode
with an argument list of type(Codable)
.
How to declare codable variable that JSONEncoder will be taking without error?
struct Me: Codable {
let id: Int
let name: String
}
var codable: Codable? // It must be generic type, but not Me.
codable = Me(id: 1, name: "Kobra")
let data = try? JSONEncoder().encode(codable!)
Here is similar question how to pass Codable using function. But I am looking how to set Codable using variable (class variable).