I am having a problem decoding some JSON received via an API. One of the fields is an optional and it is this that seems to cause the fault. The first parsed notification is always corrupt unless read_at is null.
So
struct NotificationHead: Decodable, Identifiable {
var id: Int
var read_at: String?
var subject: String
init(id: Int,
read_at: String?,
subject: String) {
self.id = id
self.subject = subject
self.read_at = read_at
}
static func parse(from json: String?) throws -> [NotificationHead] {
let mess = "[{\"id\":646,\"read_at\":null,\"subject\":\"Migration \"}, {\"id\":693,\"read_at\":\"2023-04-09T16:27:11.220Z\",\"subject\":\"test\"}, {\"id\":688,\"read_at\":\"2023-04-07T17:06:10.172Z\",\"subject\":\"test message rgerg\"},{\"id\":646,\"read_at\":null,\"subject\":\"Migration \"}]"
let notifications:[NotificationHead] = try JSONDecoder().decode([NotificationHead].self, from:mess.data(using: .utf8)!)
return notifications
}
}
In this case the notifications result is fine. But if the first item in the JSON has a populated read_at field, then the first parsed notification has 'some' as the read_at value, not the date_time string, and subsequent use of the notification causes a memory failure
So trying to parse
let mess = "[{"id":693,"read_at":"2023-04-09T16:27:11.220Z","subject":"test"}, {"id":688,"read_at":"2023-04-07T17:06:10.172Z","subject":"test message rgerg"},{"id":646,"read_at":null,"subject":"Migration "}]"
Causes the problem. I can swap the items around and as long as the first notification has a read_at value, then the first parsed notification ALWAYS has the read_at as 'some', and all subsequent notifications are parsed correctly.
Any ideas please? I'm new to Swift and this has me flumuxed!