0

when I output the test result in my application, I get

fatal error: array index out of range

In the code I marked the place where the error occurred. What could be the cause of the error?

import UIKit
import RealmSwift

class ResultVC: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var message = ""
    var result = 0
    var testedVerbs = [Verb]()
    var repeatTestedVerbs = [Verb]()

    @IBOutlet weak var resultLabel:UILabel!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var repeatVerbs: UILabel!
    @IBOutlet weak var nextButton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.reloadData()
        setUp()
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return testedVerbs.isEmpty ? 0 : testedVerbs.count
    }

    private func setUp() {
        nextButton.setUpButton(button: nextButton, color: white, tintColor: black, cornerRadius: 12)

        for verb in testedVerbs {
            if verb.progress <= 0.49 {
                repeatTestedVerbs.append(verb)
            }
        }

        if repeatTestedVerbs.count <= 3 {//testedVerbs.count/20 * 100 {
            view.backgroundColor = orange
            message = "You Can Do Better!"
        } else {
            view.backgroundColor = green
            message = "Good Job!"
        }
        resultLabel.text = "\(message)  \(result) / \(testedVerbs.count)"
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        tableView.backgroundColor = .clear
        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! ResultCell
        //        MARK: Index out of range
        let verb = repeatTestedVerbs[indexPath.row]
        cell.infinitiv.text = verb.infinitiv
        cell.translate.text = verb.translate
        cell.level.text = verb.level
        cell.backgroundColor = .clear
        return cell
    }

    static func storyboardInstance() -> ResultVC? {
        let storyboard = UIStoryboard(name: String(describing: self), bundle: nil)
        return storyboard.instantiateInitialViewController() as? ResultVC
    }

    @IBAction func repeatButton(_ sender: UIButton) {
        if let nvc = navigationController {
            nvc.popViewController(animated: true)
        }
    }

    @IBAction func cancel(_ sender:UIButton) {
        if let nvc = navigationController {
            for vc in nvc.viewControllers {
                if vc is ThemeTVC {
                    navigationController?.popToViewController(vc, animated: true)
                    break
                }
            }
        }
    }
}

1 Answer 1

1

Your numberOfRows is based on testedVerbs, but your cellForRowAt reads from repeatTestedVerbs.

Sign up to request clarification or add additional context in comments.

1 Comment

@Max if this answer helped you in solving your issue you should consider upvote it and accept it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.