- Codable JSON데이터를 쉽게 처리할 수 있다. 이거만 알고 사용하면 될꺼같음
 
기존의 JSON 데이터 처리방식
if let resultData = jsonResult.value(forKey: "data") as! NSDictionary {
    let name = resultData.value(forKey: "name") as? String ?? ""
    let id = resultData.value(forKey: "id") as? String ?? ""
    let age = resultData.value(forKey: "age") as? Int ?? ""
}
Codable적용 후
struct Info: Codable {
    var name: String?
    var id: String?
    var age: Int?
}
let data = """
{
    "name": "aa",
    "id": "bb",
    "age": \(20)
}
""".data(using: .utf8)!
let result = try! JSONDecoder().decode(Info.self, from: data)
print(result)
특정 키값이 내려오지 않을때와 null값이 내려올때도 처리 할 수 있다.
struct Info: Codable {
    var name: String?
    var id: String?
    var age: Int?
    //data에 키값이 다르면 CodingKey값을 이용해서 Codable에 넣을 수 있다.
    enum StringKeys: String, CodingKey {
        case name = "testName"
        case id = "testId"
        case age = "testAge"
    }
    init(from decoder: Decoder) throws {
        let stringValues = try decoder.container(keyedBy: StringKeys.self)
        //data에 null값이 들어올경우에는 초기값을 설정해줄 수 있다.
        name = (try? stringValues.decode(String.self, forKey: .name)) ?? "nullName"
        id = (try? stringValues.decode(String.self, forKey: .id)) ?? "nullId"
        age = (try? stringValues.decode(Int.self, forKey: .age)) ?? 0
    }
}
let data = """
{
    "testName": null,
    "testId": null,
    "testAge": null
}
""".data(using: .utf8)!
let result = try! JSONDecoder().decode(Info.self, from: data)
print(result)
