Thread 1: Fatal error: Index out of range

I am trying to show items in my array quizData[i]. And it is giving me the error Thread 1: Fatal error: Index out of range.

 static var currentIndex = 0   static var currentIndex2 = 0       static func createGameModel(i: Int, j: Int) -> Quiz {     return Quiz(currentQuestionIndex: i, quizModel: quizData[i], quizModel2: quizData2[j], quizCompleted: false)   }   @Published var model = GameManagerVM.createGameModel(i: GameManagerVM.currentIndex, j: GameManagerVM.currentIndex2)

I tried to change my if statement to GameManagerVM.quizData.count and it s still giving me the error.

 DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {           if (GameManagerVM.currentIndex < GameManagerVM.quizData.count) {             GameManagerVM.currentIndex = GameManagerVM.currentIndex + 1             self.model = GameManagerVM.createGameModel(i: GameManagerVM.currentIndex, j: GameManagerVM.currentIndex2)           } else {             self.model.quizCompleted = true             self.model.quizWinningStatus = true             self.reset()

Answered by Marcus94 in 740689022
extension GameManagerVM {
   
  static var quizData: [QuizModel] {
    [
      QuizModel(name: "Mercury", question: "Which planet is closest to the sun?", answer: "B", optionsList: [QuizOption(id: 11, optionId: "A", option: "Venus", color: Color.orange),
                                               QuizOption(id: 12, optionId: "B", option: "Mercury", color: Color.gray),
                                               QuizOption(id: 13, optionId: "C", option: "Mars", color: Color.red),
                                               QuizOption(id: 14, optionId: "D", option: "Earth", color: Color.blue)]),
     
    QuizModel(name: "Mercury", question: "Which planet is the hottest in our Solar System?", answer: "C", optionsList: [QuizOption(id: 21, optionId: "A", option: "Mars", color: Color.red),
                                                      QuizOption(id: 22, optionId: "B", option: "Earth", color: Color.blue),
                                                      QuizOption(id: 23, optionId: "C", option: "Venus", color: Color.orange),
                                                      QuizOption(id: 24, optionId: "D", option: "Jupiter", color: Color.yellow)]),
     
    QuizModel(name: "Mercury", question: "Which planet is the largest in our Solar System?", answer: "D", optionsList: [QuizOption(id: 31, optionId: "A", option: "Mars", color: Color.red),
                                                      QuizOption(id: 32, optionId: "B", option: "Earth", color: Color.blue),
                                                      QuizOption(id: 33, optionId: "C", option: "Venus", color: Color.orange),
                                                      QuizOption(id: 34, optionId: "D", option: "Jupiter", color: Color.yellow)]),
       
      QuizModel(name: "Mercury", question: "Why can't we colonize Mercury?", answer: "C", optionsList: [QuizOption(id: 41, optionId: "A", option: "Too Cold", color: Color.red),
                                                        QuizOption(id: 42, optionId: "B", option: "Mercury has no surface", color: Color.blue),
                                                        QuizOption(id: 43, optionId: "C", option: "Atmosphere is too thin", color: Color.orange),
                                                        QuizOption(id: 44, optionId: "D", option: "Too far away from the sun", color: Color.yellow)]),
      QuizModel(name: "Mercury", question: "What is the name of the first NASA spacecraft sent to Mercury?", answer: "A", optionsList: [QuizOption(id: 51, optionId: "A", option: "Mariner 10", color: Color.red),
                                                        QuizOption(id: 52, optionId: "B", option: "Voyager 2", color: Color.blue),
                                                        QuizOption(id: 53, optionId: "C", option: "Hubble Space Telescope", color: Color.orange),
                                                        QuizOption(id: 54, optionId: "D", option: "Viking 1", color: Color.yellow)])
    ]
  }

Please format code with code formatter tool:

static var currentIndex = 0
static var currentIndex2 = 0
static func createGameModel(i: Int, j: Int) -> Quiz {
    return Quiz(currentQuestionIndex: i, quizModel: quizData[i], quizModel2: quizData2[j], quizCompleted: false)
}

@Published var model = GameManagerVM.createGameModel(i: GameManagerVM.currentIndex, j: GameManagerVM.currentIndex2)
//I tried to change my if statement to GameManagerVM.quizData.count and it s still giving me the error.
 
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
    if (GameManagerVM.currentIndex < GameManagerVM.quizData.count) {
        GameManagerVM.currentIndex = GameManagerVM.currentIndex + 1
        self.model = GameManagerVM.createGameModel(i: GameManagerVM.currentIndex, j: GameManagerVM.currentIndex2)
    } else {
        self.model.quizCompleted = true
        self.model.quizWinningStatus = true
        self.reset()

We do not see in code where you populate quizData. Please show this part of code. You should first test for values:

static func createGameModel(i: Int, j: Int) -> Quiz {
    print("i", i, "j", j, "quizData", quizData.count)  // To see what values are
    return Quiz(currentQuestionIndex: i, quizModel: quizData[i], quizModel2: quizData2[j], quizCompleted: false)
}

Then you could return optional, that will let you test later

static func createGameModel(i: Int, j: Int) -> Quiz? {
    print("i", i, "j", j, "quizData", quizData.count)  // To see what values are
    if i < quizData.count && j < quizData.count {
        return Quiz(currentQuestionIndex: i, quizModel: quizData[i], quizModel2: quizData2[j], quizCompleted: false)
    }

    return nil // It failed
}
Accepted Answer
extension GameManagerVM {
   
  static var quizData: [QuizModel] {
    [
      QuizModel(name: "Mercury", question: "Which planet is closest to the sun?", answer: "B", optionsList: [QuizOption(id: 11, optionId: "A", option: "Venus", color: Color.orange),
                                               QuizOption(id: 12, optionId: "B", option: "Mercury", color: Color.gray),
                                               QuizOption(id: 13, optionId: "C", option: "Mars", color: Color.red),
                                               QuizOption(id: 14, optionId: "D", option: "Earth", color: Color.blue)]),
     
    QuizModel(name: "Mercury", question: "Which planet is the hottest in our Solar System?", answer: "C", optionsList: [QuizOption(id: 21, optionId: "A", option: "Mars", color: Color.red),
                                                      QuizOption(id: 22, optionId: "B", option: "Earth", color: Color.blue),
                                                      QuizOption(id: 23, optionId: "C", option: "Venus", color: Color.orange),
                                                      QuizOption(id: 24, optionId: "D", option: "Jupiter", color: Color.yellow)]),
     
    QuizModel(name: "Mercury", question: "Which planet is the largest in our Solar System?", answer: "D", optionsList: [QuizOption(id: 31, optionId: "A", option: "Mars", color: Color.red),
                                                      QuizOption(id: 32, optionId: "B", option: "Earth", color: Color.blue),
                                                      QuizOption(id: 33, optionId: "C", option: "Venus", color: Color.orange),
                                                      QuizOption(id: 34, optionId: "D", option: "Jupiter", color: Color.yellow)]),
       
      QuizModel(name: "Mercury", question: "Why can't we colonize Mercury?", answer: "C", optionsList: [QuizOption(id: 41, optionId: "A", option: "Too Cold", color: Color.red),
                                                        QuizOption(id: 42, optionId: "B", option: "Mercury has no surface", color: Color.blue),
                                                        QuizOption(id: 43, optionId: "C", option: "Atmosphere is too thin", color: Color.orange),
                                                        QuizOption(id: 44, optionId: "D", option: "Too far away from the sun", color: Color.yellow)]),
      QuizModel(name: "Mercury", question: "What is the name of the first NASA spacecraft sent to Mercury?", answer: "A", optionsList: [QuizOption(id: 51, optionId: "A", option: "Mariner 10", color: Color.red),
                                                        QuizOption(id: 52, optionId: "B", option: "Voyager 2", color: Color.blue),
                                                        QuizOption(id: 53, optionId: "C", option: "Hubble Space Telescope", color: Color.orange),
                                                        QuizOption(id: 54, optionId: "D", option: "Viking 1", color: Color.yellow)])
    ]
  }
Thread 1: Fatal error: Index out of range
 
 
Q