Slide 5
Slide 5 text
᭗ଉαϘτϰ϶ασЄ
enum Bar: String, Decodable {
case case1, case2
}
struct Foo: Decodable {
let array: [Int]
let string: String
let int: Int
let double: Double
let bar: Bar
private enum CodingKeys: CodingKey {
case array, string, int, double, bar
}
init(from decoder: Decoder) throws {
let container = decoder.container(keyedBy: CodingKeys.self)
array = decoder.decodeIfPresent([Int].self, forKey: .array) ?? []
string = decoder.decodeIfPresent(String.self, forKey: .string) ?? ""
int = decoder.decodeIfPresent(Int.self, forKey: .int) ?? 0
double = decoder.decodeIfPresent(Double.self, forKey: .double) ?? -1
bar = decoder.decodeIfPresent(Bar.self, forKey: .bar) ?? .case2
}
init(array: [Int] = [], string: String = "", int: Int = 0, double: Double = -1, bar: Bar = .case2) {
self.array = array
self.string = string
self.int = int
self.double = double
self.bar = bar
}
}