0

I have one Swift Codable model like that:

public struct model: Codable {
    public let id: String
    public let title: String

    public var _tag: MyTag? = MyTag.defaultValue
}

The API JSON not contains _tag field. And I set _tag to default value and change/check it later in client side.

I'm pretty sure that field could not be nil. However if remove '?' mark of public var _tag: Tag?. Moya JSONDecoder report that error: Swift.DecodingError.keyNotFound.

Any workaround to set that field with default value without Optional type and also could decode from JSON without any error?

Thanks.

user25917
  • 797
  • 7
  • 18
  • 1
    Is this what you're looking for? [With JSONDecoder in Swift 4, can missing keys use a default value instead of having to be optional properties?](https://stackoverflow.com/questions/44575293/with-jsondecoder-in-swift-4-can-missing-keys-use-a-default-value-instead-of-hav) – Robert Dresler Mar 05 '19 at 07:20
  • Sure. Thanks @Robert Dresler – user25917 Mar 05 '19 at 07:23

0 Answers0