I am developing an iOS app in Swift with a coworker.
I have an API I am using to retrieve some chats from a server. Every chat has a field message_id
. I expect this to be a string, but the API sometimes gives it to me as an integer. Here is a sample of the JSON:
[
{
"id": 17,
"message_id": "434",
"last_sender_id": "121",
"subject": {
"rendered": "Re: No Subject"
},
"excerpt": {
"rendered": "very good"
},
"message": {
"rendered": "<p>very good</p>\n"
},
"date": "2023-06-21T14:32:47",
},
{
"id": 17,
"message_id": 430,
"last_sender_id": "121",
"subject": {
"rendered": "Re: No Subject"
},
"excerpt": {
"rendered": "Hi"
},
"message": {
"rendered": "<p>Hi</p>\n"
},
"date": "2022-11-18T15:20:13",
},
{
"id": 17,
"message_id": "428",
"last_sender_id": "121",
"subject": {
"rendered": "No Subject"
},
"excerpt": {
"rendered": "Test"
},
"message": {
"rendered": "<p>Test</p>\n"
},
"date": "2022-11-18T15:19:10",
},
]
The API provider has confirmed this as a bug to fix in the future, but I have to find a workaround for it now. I asked my coworker can we not just convert the type from int to string when using it? (I am not the Swift expert). They said:
"that's not how that works, since I am getting
message_id
from JSON and I have to tell the converter before all objects are unpacked what to expect".
Is what they said correct? How do I workaround this bug?
Called the API. It sometimes gives the message_id
as string, sometimes integers. This is a bug I need to work around for now until the provider fixes it. I have updated OP with what my co-worker says.