How to exclude properties from Swift 4's Codable

后端 未结 6 1248
感情败类
感情败类 2020-12-23 15:40

Swift 4\'s new Encodable/Decodable protocols make JSON (de)serialization quite pleasant. However, I have not yet found a way to have fine-grained c

6条回答
  •  春和景丽
    2020-12-23 16:04

    If we need to exclude decoding of a couple of properties from a large set of properties in the structure, declare them as optional properties. Code to unwrapping optionals is less than writing a lot of keys under CodingKey enum.

    I would recommend using extensions to add computed instance properties and computed type properties. It separates codable comforming properties from other logic hence provides better readability.

提交回复
热议问题