Sunday, November 27, 2022
HomeiOS Developmentios - Swift Index Out of Vary after API name

ios – Swift Index Out of Vary after API name


I am attempting to fetch some information from an API, nonetheless an Index out of vary error is returning most likely as a result of I’m attempting to entry the array earlier than they’re populated.

I’ve tried some ways however I’m nonetheless getting the identical error, can anybody recommend some options?

Here is the code for fetching the info and populating the array to show them contained in 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() {
        tremendous.viewDidLoad()
        // Do any extra 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.major.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].group.identify)
                self.pointsArray.append(String(standings.clubName[i][j].factors))
                self.gamesPlayedArray.append(String(standings.clubName[i][j].all.performed))
                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].group.emblem))
            }
        }
    }
}

//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.textual content = teamsArray[indexPath.row]
        cell.label2.textual content = gamesPlayedArray[indexPath.row]
        cell.label3.textual content = winsArray[indexPath.row]
        cell.label4.textual content = drawsArray[indexPath.row]
        cell.label5.textual content = deafeatsArray[indexPath.row]
        cell.label6.textual content = pointsArray[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection part: Int) -> Int {
        return 20;
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments