迅速解析api的TIC读取状态

时间:2018-07-27 13:31:44

标签: ios swift api

我正在使用localhost Api的IOS应用程序上工作。以下是我的代码:

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()


}

上面的代码工作正常,我正在将所有数据加载到表视图中。 我希望当用户选择一行时,它应该调用另一个Api以在另一个ViewController上显示所选屏幕的更多细节。但是当我呼叫第二个Api时,出现以下错误:

 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)
}

在我的第二次Api呼叫中,出现了类似TeamApiScreen [34251:281435]的错误TIC读取状态[5:0x600000178540]:1:57

0 个答案:

没有答案