Home > Blockchain >  Swift Index Out of Range after API call
Swift Index Out of Range after API call

Time:09-26

I'm trying to fetch some data from an API, however an Index out of range error is returning probably because I am trying to access the array before they are populated.

I have tried many ways but I am still getting the same error, can anyone suggest some solutions?

Here's the code for fetching the data and populating the array to display them inside the TableView cells

@IBOutlet weak var tableView: UITableView!
    
    var teamsArray = [String]()
    var pointsArray = [String]()
    var winsArray = [String]()
    var drawsArray = [String]()
    var deafeatsArray = [String]()
    var gamesPlayedArray = [String]()
    var urlArray = [String]()

    
    var footballManager = FootballManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        footballManager.delegate = self
        footballManager.performRequest()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(UINib(nibName: "StandingsCell", bundle: nil), forCellReuseIdentifier: "standingsCell")
    }
}


//MARK: - FootballManagerDelegate

extension ViewController: FootballManagerDelegate {
    func didUpdateStandings(_ footballManager: FootballManager, standings: FootballModel) {
        DispatchQueue.main.async {
            //self.clubsArray = standings.clubName
            self.populateArray(standings: standings)
            self.tableView.reloadData()
        }
    }
    func didFailWithError(error: Error) {
        print(error)
    }
    
    func populateArray(standings: FootballModel){
        for i in 0..<1 {
            for j in 0...standings.clubName[0].count-1 {
                self.teamsArray.append(standings.clubName[i][j].team.name)
                self.pointsArray.append(String(standings.clubName[i][j].points))
                self.gamesPlayedArray.append(String(standings.clubName[i][j].all.played))
                self.drawsArray.append(String(standings.clubName[i][j].all.draw))
                self.winsArray.append(String(standings.clubName[i][j].all.win))
                self.deafeatsArray.append(String(standings.clubName[i][j].all.lose))
                self.urlArray.append(String(standings.clubName[i][j].team.logo))
            }
        }
    }
}

//MARK: - UITabelViewDelegate

extension ViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "standingsCell", for: indexPath) as! StandingsCell
        cell.clubLogo.load(url: URL(string: urlArray[indexPath.row])!)
        cell.label1.text = teamsArray[indexPath.row]
        cell.label2.text = gamesPlayedArray[indexPath.row]
        cell.label3.text = winsArray[indexPath.row]
        cell.label4.text = drawsArray[indexPath.row]
        cell.label5.text = deafeatsArray[indexPath.row]
        cell.label6.text = pointsArray[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 20;
    }
}

CodePudding user response:

You need to change numberOfRowsInSection to reflect current size of the array ,also you need to use one array instead of multiple data source arrays

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return teamsArray.count 
}

Warning: here i assume that all arrays are of the same size

  • Related