I am creating an app wherein it pulls PatientList
from API Server and it will display to a TableView
. Upon checking, it returns 200
status code but falls to invalidJSON
error. But when I checked in Postman, it returns 200
status code and pulls the records properly. I am quite confuse which part of my codes causes the error since I am new in swift. I am seeking help to solve the issue. Below are my sample codes for your references:
Patient.swift
struct Patient: Codable { let hospitalNumber: Int let patientName: String let totalAmount: Doubleenum CodingKeys: String, CodingKey { case hospitalNumber = "hospitalNumber" case patientName = "patientName" case totalAmount = "totalAmount" }}
APIService.swift
struct PatientList { typealias getPatientListTaskCompletion = (_ patientListperPayout: [Patient]?, _ error: NetworkError?) -> Void static func getPatientList(doctorNumber: Int, periodId: Int, completion: @escaping getPatientListTaskCompletion) { guard let patientPerPayoutURL = URL(string: "\(Endpoint.Patient.patientPerPayout)?periodId=\(periodId)&doctorNumber=\(doctorNumber)") else { completion(nil, .invalidURL) return } let sessionManager = Alamofire.SessionManager.default sessionManager.session.getAllTasks { (tasks) in tasks.forEach({ $0.cancel() }) } Alamofire.request(patientPerPayoutURL, method: .get, encoding: JSONEncoding.default).responseJSON { (response) in guard HelperMethods.reachability(responseResult: response.result) else { completion(nil, .noNetwork) return } guard let statusCode = response.response?.statusCode else { completion(nil, .noStatusCode) return } switch(statusCode) { case 200: guard let jsonData = response.data else{ completion(nil, .invalidJSON) print(statusCode) return } let decoder = JSONDecoder() do { let patientListArray = try decoder.decode([Patient].self, from: jsonData) let sortedPatientListArray = patientListArray.sorted(by: { $0.patientName < $1.patientName }) completion(sortedPatientListArray, nil) }catch{ completion(nil, .invalidJSON) print(statusCode) } case 400: completion(nil, .badRequest) case 404: completion(nil, .noRecordFound) default: print("UNCAPTURED STATUS CODE FROM getPatientList\nSTATUS CODE: \(statusCode)") completion(nil, .uncapturedStatusCode) } } }
Controller.swift
var patientList: [Patient]! { didSet { performSegue(withIdentifier: patientListIdentifier, sender: self) }}override func viewDidLoad() { super.viewDidLoad() self.latestCreditedAmountTableView.dataSource = self self.latestCreditedAmountTableView.delegate = self configureTableViewCell() showTotalCreditedAmount() getDoctorPayoutSummary(doctorNumber: doctorNumber)}func getDoctorPayoutSummary(doctorNumber: Int) { self.payoutSummary = payoutSummaryDetails self.taxRateVatRateLabel.text = "\(self.payoutSummary.taxRate) / \(self.payoutSummary.vatRate)" self.getPatientList() self.latestCreditedAmountTableView.reloadData() return} func getPatientList() { APIService.PatientList.getPatientList(doctorNumber: doctorNumber, periodId: currentRemittance.periodId) { (patientListArray, error) in guard let patientListPerPayout = patientListArray, error == nil else { if let networkError = error { switch networkError { case .noRecordFound: let alertController = UIAlertController(title: "No Record Found", message: "You don't have current payment remittance", preferredStyle: .alert) alertController.addAction(UIAlertAction(title: "OK", style: .default)) self.present(alertController, animated: true, completion: nil) case .noNetwork: let alertController = UIAlertController(title: "No Network", message: "\(networkError.rawValue)", preferredStyle: .alert) alertController.addAction(UIAlertAction(title: "OK", style: .default)) self.present(alertController, animated: true, completion: nil) default: let alertController = UIAlertController(title: "Error", message: "There is something went wrong. Please try again", preferredStyle: .alert) alertController.addAction(UIAlertAction(title: "OK", style: .default)) self.present(alertController, animated: true, completion: nil) } } return } self.patientList = patientListPerPayout return }}
JSON Response
[ {"hospitalNumber": null,"patientName": null,"totalAmount": 31104 }, {"hospitalNumber": "","patientName": "LastName, FirstName","totalAmount": 3439.8 }]