尝试直接转到数组末尾时发生索引超出范围错误

Index out of range error when attempting to go directly to the end of an array

这是我在Xcode项目中的数组。

Var questions = ["q.1", "q.2", "q.3"]

我添加了两个按钮,即前进按钮和后退按钮,用于滚动浏览问题。现在,当我在模拟器上启动我的项目时,当我第一次点击后退按钮时,它会直接将我带到 "q.3",它给我一个错误提示:

"Index out of range." 

请记住,当我最初点击前进按钮时它工作正常并带我 "q.2" 但是当我最初点击后退按钮时它给我一个错误。下面是我的代码。

@IBOutlet weak var question: UILabel! //this is my question label 
questions = ["q.1", "q.2", "q.3"] //this is my array of questions

currentQuestionIndex = 0

@IBAction func backwardBtnTapped(_ sender: Any) {

currentQuestionIndex -= 1
let noOfQuestions = questions.count
let previousQuestionIndex = currentQuestionIndex % noOfQuestions
question.text = questions[previousQuestionIndex]

@IBAction func forwardBtnTapped(_ sender: Any) {

currentQuestionIndex += 1
let noOfQuestions =  questions.count
let nextQuestionIndex =  currentQuestionIndex % noOfQuestions
question.text =  questions[nextQuestionIndex] 

像这样尝试可能会有帮助

@IBAction func backwardBtnTapped(_ sender: Any) {

       currentQuestionIndex -= 1
       if currentQuestionIndex < 0
       {
          currentQuestionIndex = questions.count-1
       }
       question.text = questions[currentQuestionIndex]
    }

@IBAction func forwardBtnTapped(_ sender: Any) {

    currentQuestionIndex += 1
    if currentQuestionIndex >= questions.count
    {
       currentQuestionIndex = 0
    }
    question.text =  questions[currentQuestionIndex]
} 

您需要检查条件。

if currentQuestionIndex > 0{
    let noOfQuestions = questions.count
    let previousQuestionIndex = currentQuestionIndex % noOfQuestions
    question.text = questions[previousQuestionIndex]
  }

如果您希望分别在最后一个和第一个问题上禁用前进和后退按钮。您可以执行以下操作:

假设您的按钮出口名为 forwardButton 和 backwardButton 将其初始化为:

backwardButton.userInteractionEnabled = NO

并将您的前进和后退按钮的 IBAction 设为:

@IBAction func backwardBtnTapped(_ sender: Any) {

    currentQuestionIndex -= 1
    if currentQuestionIndex == 0 {
        backwardButton.userInteractionEnabled = NO
    }
    forwardButton.userInteractionEnabled = YES
    let noOfQuestions = questions.count
    let previousQuestionIndex = currentQuestionIndex % noOfQuestions
    question.text = questions[previousQuestionIndex]
}

@IBAction func forwardBtnTapped(_ sender: Any) {

    currentQuestionIndex += 1
    if currentQuestionIndex >= questions.count - 1 {
        forwardButton.userInteractionEnabled = NO
    }
    backwardButton.userInteractionEnabled = YES
    let noOfQuestions =  questions.count
    let nextQuestionIndex =  currentQuestionIndex % noOfQuestions
    question.text =  questions[nextQuestionIndex] 
}

或者,如果您希望它像现在一样运行,只需在 IBAction 中为前进和后退按钮执行此操作:

@IBAction func backwardBtnTapped(_ sender: Any) {

    currentQuestionIndex -= 1
    if currentQuestionIndex < 0 {
        currentQuestionIndex = questions.count - 1
    }
    question.text = questions[currentQuestionIndex]
}

@IBAction func forwardBtnTapped(_ sender: Any) {

    currentQuestionIndex += 1
    if currentQuestionIndex >= questions.count {
        currentQuestionIndex = 0
    }
    question.text =  questions[currentQuestionIndex] 
}