“The data couldn’t be read because it is missing” error when decoding JSON in Swift

后端 未结 5 580
温柔的废话
温柔的废话 2021-01-31 07:20

I am getting the following error :

The data couldn’t be read because it is missing.

When I run the following code:

struct Indicator: Decodable {
         


        
5条回答
  •  旧巷少年郎
    2021-01-31 08:12

    First make the properties optional then

    If your case similar to this try this decodeIfPresent

        `public init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        firstName = try container.decodeIfPresent(String.self, forKey: .firstName)
        }`
    

提交回复
热议问题