I am using NSUrlConnection and Codable library of Apple.
I am using a web API to signup new users for my application. The API returns a status and a message (in json which i map to a model).
This is my model class:
Struct SignUpResult {
let message: String
let status: String
}
Struct SignUpParams {
let name: String
let email: String
let mobile_no: String
let password: String
}
If the user gave all the parameters correctly then the message is returned as a string. Like this:
{
"status": "OK",
"message": "User signup successfully"
}
On the other hand, if the user entered the parameters incorrectly then the message is returned as an array. Like this:
{
"status": "INVALID_PARAMS",
"message": [
"The name may only contain letters."
]
}
If the parameters are incorrect then i get the error
"expected to decode a string but found an array instead".
This is the code i get the error on:
let result = JSONDecoder().decode(SignUpResult.self, from: data)
What should i do?