'Loading Date-Object from JSON would not work
I read a JSON-String from my api with a Date-Array.
[
{
"id": 1,
"headline": "How to messure the T8",
"descriptiontext": "Learn how to messure the T8",
"adddate": {
"date": "2022-03-02 00:00:00.000000",
"timezone_type": 3,
"timezone": "UTC"
},
"changedate": {
"date": "2022-03-02 00:00:00.000000",
"timezone_type": 3,
"timezone": "UTC"
}
}
]
I had add a datemodel to the the class. without the date my decoding works and the objects would be viewed. But it seems that i have a fail in my decoding of the date.
Here is my struct
// MARK: - TutorialModel
struct TutorialModel: Identifiable, Codable {
let id: Int
let adddate, changedate: CustomDateModel
let headline, descriptiontext: String
}
// MARK: - CustomDateModel
struct CustomDateModel: Codable {
let date: String
let timezoneType: Int
let timezone: String
enum CodingKeys: String, CodingKey {
case date
case timezoneType
case timezone
}
}
And here is the part, were i try to Decode the JSON
guard let newTutorials = try? JSONDecoder().decode([TutorialModel].self, from: data) else { return }
DispatchQueue.main.async { [weak self] in
self?.tutorials = newTutorials
}
Could it be, that I have to read the data in a other way?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
