I am using alamofire for service integration. its giving an empty reponse as [] sometimes only. in that time app is crashing because of empty array response. How to handle that response in swift iOS.
Here is my code:
let headers = ["Authorization" : "Bearer "+token,
"Content-Type": "application/json"]
Alamofire.request("http://sos.partnersbuddy.in/api/friend_request/received", method: .get, encoding: JSONEncoding.default, headers: headers).responseJSON { response in // print("Request (response.request)")
print("RESPONSE \(String(describing: response.result.value))")
print("RESPONSE \(response.result)")
print("RESPONSE \(response)")
var respVO:[RequestResvo] = Mapper<RequestResvo>().mapArray(JSONArray: response.result.value as! [[String : Any]])
print(respVO)
self.postID = respVO[0].id!
if let result = response.result.value {
let JSON = result as! NSArray
print(JSON.value(forKey: "user"))
let res = JSON.value(forKey: "user")
let respVo = Mapper<SubRequestVo>().mapArray(JSONArray: res as! [[String : Any]])
for (index, element) in (respVo.enumerated()) {
print(index)
self.nameArr.append(element.first_name!)
self.numberArr.append(element.mobile!)
print("nameArr\(self.nameArr)")
print("numberArr\(self.numberArr)")
DispatchQueue.main.async {
// update your UI and model objects here
self.mytableView.reloadData()
// SKActivityIndicator.dismiss()
}
}
}
}
How to handle this empty response in that time app is crashing because of empty array response. How to handle that response in swift iOS.