-1

Based on server response I have created this struct items now the value changed in server how to validate those in struct.

Example yearValue replaced with eraValue.

How the frontend should handle the pair is modified or removed.

//Mark:- Time Values
struct TimeValue: Codable{
    let  yearValue,monthValue,dayValue : String

}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
KkMIW
  • 1,092
  • 1
  • 17
  • 27

2 Answers2

0

You cannot validate and change the struct at runtime.

Catch the DecodingError, print it, read it, fix the issue instantly and rebuild the app.

It's much more comfortable than fixing JSONSerialization issues.

vadian
  • 274,689
  • 30
  • 353
  • 361
0

Using Codable

struct TimeValue: Codable {

can't predict the future , you shouldn't play with server keys for an alive app as this will cause decoding errors and will give UN-expected results to users , you can make values optional

let  yearValue,monthValue,dayValue : String?

and check it's null ability before usage but it's not a good way for an app that should be stable in case you need all values non-null

The moment you change the server keys should be followed directly with a front end change with the latest keys

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87