I am having a struct which conforms to the protocol Codable. I am having a property of type [String: Any]?. But the codable doesn't allow me to use it. Saying the error
does not conform to protocol 'Decodable
Use the old JSONSerialization
class to convert between Data
and [String: Any]
if you must. Data
is Codable. You could also use another format, just as String
. Note that swift is strongly typed, so using an enum with associated values is generally preferred over Any
. If the intent is to actually write to the sever and not to local storage then you can also consider just forgetting about Codable and using JSONSerialization for the whole thing.
Example:
import UIKit
import PlaygroundSupport
struct A: Codable {
let a: Int
let b: [String: Any]
enum CodingKeys: String, CodingKey {
case a
case b
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
a = try values.decode(Int.self, forKey: .a)
let data = try values.decode(Data.self, forKey: .b)
b = try JSONSerialization.jsonObject(with: data, options: []) as! [String: Any]
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(a, forKey: .a)
let data = try JSONSerialization.data(withJSONObject: b, options: [])
try container.encode(data, forKey: .b)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With