I'm working on IOS app in which I'm using localhost Api. Following is my code:
override func viewDidLoad() {
super.viewDidLoad()
guard let url = URL(string: "http://127.0.0.1:8000/api/teams") else {return}
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let dataResponse = data,
error == nil else {
print(error?.localizedDescription ?? "Response Error")
return }
do{
//here dataResponse received from a network request
let jsonResponse = try JSONSerialization.jsonObject(with:
dataResponse, options: [])
guard let jsonArray = jsonResponse as? [[String: Any]] else {
return
}
for team in jsonArray{
guard let name = team["name"] as? String else { return }
guard let country = team["country"] as? String else{return}
self.teamName.append(name)
self.teamCountry.append(country)
print(name,country) //Output
}
} catch let parsingError {
print("Error", parsingError)
}
self.tableView.reloadData()
}
task.resume()
}
The above code is working fine and I'm getting all data loaded in my tableview. I want that when user select a row it should call another Api to show more detail of the selected screen on another viewcontroller. But when I call my second Api I get following error:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let DvC = storyBoard.instantiateViewController(withIdentifier: "DetailViewController") as! ViewController
guard let url = URL(string: "http://127.0.0.1:8000/api/team/"+teamName[indexPath.row]) else {return}
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
guard let dataResponse = data,
error == nil else {
print(error?.localizedDescription ?? "Response Error")
return }
do{
//here dataResponse received from a network request
let jsonResponse = try JSONSerialization.jsonObject(with:
dataResponse, options: [])
guard let jsonArray = jsonResponse as? [[String: Any]] else {
return
}
guard let name = jsonArray[0]["name"] as? String else { return }
guard let country = jsonArray[0]["country"] as? String else{ return }
DvC.getName = name
DvC.getCountry = country
} catch let parsingError {
print("Error", parsingError)
}
}
task.resume()
self.navigationController?.pushViewController(DvC, animated: true)
}
In my second Api call am getting error like TeamApiScreen[34251:281435] TIC Read Status [5:0x600000178540]: 1:57