Reputation: 535
I'm creating a login page, and I used to handle API error such as:
{"status":"error","answer":"Password is wrong"}
this way:
if error?["status"] as? String == "error" {
Alert.showBasic(title: error!["status"] as! String, message: error!["answer"] as! String, vc: self)
This worked perfectly. But now I'm working with another API, that responds to my request this way:
{"password":["password is wrong"]}
The confusion what I have, is that I don't get from API answer, a status for my request, like success or error.
Is there a way to handle it?
Upvotes: 0
Views: 185
Reputation: 193
The error != nil
depends and what you feed to your model and how you feed depends on status code
if let httpResponse = response as? HTTPURLResponse{
switch httpResponse.statusCode {
case 200 : //handle Root data model
case 401 : //handle wrong password model
}
}
URLSession error is different . Its when the request is failed and no response . But you got the response but its up to you differentiate whether its good or bad.
Server response should be static if there is a change not noticed to you means something wrong in the backend.
Upvotes: 1