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

时间:2017-05-05 12:06:25

标签: ios swift indexoutofrangeexception

这是我在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] 

3 个答案:

答案 0 :(得分:1)

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

假设您的按钮的出口名为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] 
}

答案 1 :(得分:0)

尝试相似这可能有用

@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]
} 

答案 2 :(得分:0)

您需要检查条件。

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