数组已更新,但访问时会显示旧值

时间:2019-07-22 20:14:16

标签: swift xcode function delegates

我正在使用Swift 5和Xcode 10制作一个计时器应用程序。在'HomeScreenViewController'(UIViewController)中,函数'startTimer()'添加了一个类型为'TimerObject'的项目(这是一个自定义类,用于存储计时器) )添加到数组“ activeTimers”。在startTimer()中对activeTimers进行更新,但是在函数“ timerCountDown()”中访问activeTimers时,不会更新该数组。这会导致应用崩溃。

我已经确保在调用timerCountDown()之前,对activeTimers实际上进行了更新。数组在startTimer()内部更新,可以在其中访问,但是在调用timerCountDown()时不会更新。

func getTimerRowFromID(id: Int) -> Int {
    for i in 0..<activeTimers.count {
        if activeTimers[i].id == id {
            return i
        }
    }
    return 0 // intentional crash
}

func timerCountDown(timerId: Int) {
    let indexPath = IndexPath(item: self.getTimerRowFromID(id: timerId), section: 1)
    self.theTableView.reloadRows(at: [indexPath], with: .none)
}

func startTimer(row: Int) {
    let timerToStart = TimerObject(secondsLeft: favoriteTimersTimes[row], title: favoriteTimersTitles[row], id: activeTimers.count)
    activeTimers.insert(timerToStart, at: 0)
}

致命错误:意外发现nil,同时隐式展开了Optional值 2019-07-22 21:45:59.755219 + 0200 Timer [49793:10984362]致命错误:意外发现为零,同时隐式展开了一个可选值

0 个答案:

没有答案