Swift's JSONDecoder with multiple date formats in a JSON string?

前端 未结 8 1564
既然无缘
既然无缘 2020-12-14 06:48

Swift\'s JSONDecoder offers a dateDecodingStrategy property, which allows us to define how to interpret incoming date strings in accordance with a

8条回答
  •  天命终不由人
    2020-12-14 07:48

    There is no way to do this with a single encoder. Your best bet here is to customize the encode(to encoder:) and init(from decoder:) methods and provide your own translation for one these values, leaving the built-in date strategy for the other one.

    It might be worthwhile looking into passing one or more formatters into the userInfo object for this purpose.

提交回复
热议问题