How to use Any in Codable Type

Quantum Value First of all you can define a type that can be decoded both from a String and Int value. Here it is. enum QuantumValue: Decodable { case int(Int), string(String) init(from decoder: Decoder) throws { if let int = try? decoder.singleValueContainer().decode(Int.self) { self = .int(int) return } if let string = try? decoder.singleValueContainer().decode(String.self) { … Read more

Custom Swift Encoder/Decoder for the Strings Resource Format

A bit late to the party here but I feel this might be helpful/informative to others given the question high vote count. (But I won’t really get into the actual usefulness of such code in practice—please check the comments above for that.) Unfortunately, given the coding stack flexibility and type-safeness, implementing a new encoding and … Read more