I am using response from API and parsing it as a JSON.
var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! [String: AnyObject]
let status = readableJSON["status"] as! String
if status == "success" {
if let profileInfo = readableJSON["profile_info"] {
let stb = UIStoryboard(name: "Main", bundle: nil)
let vcVerifyOTP = stb.instantiateViewController(withIdentifier: "verify_otp") as! VCVerifyOTP
vcVerifyOTP.sessionID = readableJSON["otp_session_id"] as! String
vcVerifyOTP.userID = readableJSON["customer_id"] as! String
vcVerifyOTP.first_name = profileInfo["firstname"] as! String
vcVerifyOTP.last_name = profileInfo["lastname"] as! String
vcVerifyOTP.email_id = profileInfo["email"] as! String
vcVerifyOTP.phone_number = profileInfo["mobile"] as! String
let scores = profileInfo["scores"] as! [String: AnyObject]
vcVerifyOTP.gmat = scores["gmat"] as! String
vcVerifyOTP.gre = scores["gre"] as! String
vcVerifyOTP.ielts = scores["ielts"] as! String
vcVerifyOTP.tofel = scores["tofel"] as! String
self.present(vcVerifyOTP, animated: true, completion: nil)
}
} else {
let alert = UIAlertController(title: "Error", message: "Login Unsuccessful.", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
This above code works fine if 'scores' JSONObject contains any data, but if it is a blank app crashes.
IF 'scores' CONTAINS DATA (RESPONSE IS) { "status":"success", "otp_session_id":"somevalue", "customer_id":"somevalue", "profile_info": { "firstname":"somevalue", "lastname":"somevalue", "email":"somevalue", "mobile":"somevalue", "scores": { "gmat":"somevalue", "gre":"somevalue", "ielts":"somevalue", "tofel":"somevalue" } } }
IF 'scores' DOES NOT CONTAINS DATA (RESPONSE IS) (GIVING ERROR) { "status":"success", "otp_session_id":"somevalue", "customer_id":"somevalue", "profile_info": { "firstname":"somevalue", "lastname":"somevalue", "email":"somevalue", "mobile":"somevalue", "scores":[0] } }
What happening is if there is data inside 'scores' then it comes as JSONObject and if there is no data inside 'scores' then it comes as JSONArray
Now if array comes it crashes. I tried many things but I cant catch this exception. I even tried to check if value is JSONObject or JSONArray but still no success. Please Help.