简体   繁体   中英

App Crash on Table Row load

I'm pulling data in from CloudKit, and there is only one item in the data.

Once the matchup loads , and before the background color is seen in the UI, the app crashes . I can't figure out why, any ideas?

InterfaceController on Watch:

func loadTable() {
    self.rowTable.setNumberOfRows(self.matchupArray.count, withRowType: "rows")
    let rowCount = self.rowTable.numberOfRows

    for i in 0...rowCount  {
        let row = self.rowTable.rowController(at: i) as! Rows!
        row?.matchup.setText(self.matchupArray[i])
        let colorBackground = UIColor.init(hex: self.teamColorArray[i])
        row?.groupColor.setBackgroundColor(colorBackground)
    }

}

func getData() {
    cloud.getCloudKit { (game: [GameWatch]) in
        var teamColorArray = [String]()
        var matchupArray = [String]()

        for item in game {
            teamColorArray.append(item.teamColor)
            matchupArray.append(item.matchup)
        }

        self.teamColorArray = teamColorArray
        self.matchupArray = matchupArray

        self.loadTable()
    }

}

UPDATE :

Got a crash, with the error "fatal error: Index out of range".

I'm not sure why this is, because the matchupArray.count is 1, the rowCount is 1. It started iterating through the for-loop with i as 0, and finished the first iteration where it should have stopped since there was only 1 item. But I got the crash because it started to iterate through the loop again, with i as 1, and then obviously found nothing so it crashed.

The crash comes after row?.matchup.setText(self.matchupArray[i]) is run.

The error is in the line:

for i in 0...rowCount

This ... operator creates a range of indexes that includes both values when, because Swift uses 0-based arrays, you need the ..< operator to create a range that excludes the upper value.

This line should therefore be:

for i in 0..<rowCount

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM