Swift 4は新しいCodableプロトコルを追加しました。私が使用JSONDecoderすると、CodableクラスのオプションではないすべてのプロパティにJSONのキーが必要になるか、エラーがスローされます。
私のクラスのすべてのプロパティをオプションにすることは、私が本当に望んでいるのはjsonの値またはデフォルト値を使用することなので、不要な手間のように思えます。(このプロパティをnilにしたくありません。)
これを行う方法はありますか?
class MyCodable: Codable {
    var name: String = "Default Appleseed"
}
func load(input: String) {
    do {
        if let data = input.data(using: .utf8) {
            let result = try JSONDecoder().decode(MyCodable.self, from: data)
            print("name: \(result.name)")
        }
    } catch  {
        print("error: \(error)")
        // `Error message: "Key not found when expecting non-optional type
        // String for coding key \"name\""`
    }
}
let goodInput = "{\"name\": \"Jonny Appleseed\" }"
let badInput = "{}"
load(input: goodInput) // works, `name` is Jonny Applessed
load(input: badInput) // breaks, `name` required since property is non-optional