数组索引使用按钮超出范围

时间:2016-05-01 13:34:31

标签: ios arrays swift

初学者,第一个Swift应用程序。按下nextPage按钮时,我似乎无法阻止我的阵列超出范围。

我的代码:

class ViewController: UIViewController {

    var score: Float = 0.0
    var questions: [Question] = []
    var index = 0


    @IBAction func nextPage(sender: AnyObject) {

        if questions.count < 9 {
            savesliderValues()
            incrementIndex()
            loadQuestions()
            calculatetotalScore()
        } else {
            savesliderValues()
        }
    }


    @IBOutlet weak var backbuttonOutlet: UIBarButtonItem!
    @IBAction func backButton(sender: AnyObject) {
        savesliderValues()
        decrementIndex()
        loadQuestions()
    }

    @IBOutlet weak var question1Label: UILabel!
    @IBOutlet weak var liberalA: UISlider!
    @IBAction func liberalsliderAChange(sender: UISlider) {

    }


    @IBOutlet weak var question2Label: UILabel!
    @IBOutlet weak var liberalB: UISlider!
    @IBAction func liberalsliderBChange(sender: UISlider) {

    }

    @IBOutlet weak var question3Label: UILabel!
    @IBOutlet weak var liberalC: UISlider!
    @IBAction func liberalsliderCChange(sender: UISlider) {

    }


    override func viewDidLoad() {
        super.viewDidLoad()

        questions.append(Question(text: "hello", answer: 0))
        questions.append(Question(text: "g78", answer: 0))
        questions.append(Question(text: "boo", answer: 0))
        questions.append(Question(text: "q4", answer: 0))
        questions.append(Question(text: "q5", answer: 0))
        questions.append(Question(text: "788", answer: 0))
        questions.append(Question(text: "756666", answer: 0))
        questions.append(Question(text: "jjjjj", answer: 0))
        questions.append(Question(text: "yyyyyyy", answer: 0))


        loadQuestions()
    }

    func currentQuestion1() -> Question {
        return questions[index]

    }

    func currentQuestion2() -> Question {
        return questions[index + 1]
    }

    func currentQuestion3() -> Question {
        return questions[index + 2]
    }

    func loadQuestions() {
        question1Label.text = currentQuestion1().text
        liberalA.value = currentQuestion1().answer

        question2Label.text = currentQuestion2().text
        liberalB.value = currentQuestion2().answer

        question3Label.text = currentQuestion3().text
        liberalC.value = currentQuestion3().answer
    }

    func savesliderValues(){
        currentQuestion1().answer = liberalA.value
        currentQuestion2().answer = liberalB.value
        currentQuestion3().answer = liberalC.value
    }

    func incrementIndex() {
        index = index + 3

    }

    func decrementIndex() {
        index = index - 3
    }


    func calculatetotalScore() {
        let totalScore = questions.reduce(0) { (currentvalue, question) -> Float in
            currentvalue + question.answer
        }
        print(totalScore)
    }

}

1 个答案:

答案 0 :(得分:3)

func currentQuestion3() -> Question {
    return questions[index + 2]
}
  

因为此函数使用索引访问问题数组   当前+ 2,您需要调整下一个按钮代码,如下所示

// count isnt 0 based so deduct 1
if index + 2 <= questions.count - 1 {
        savesliderValues()
        incrementIndex()
        loadQuestions()
        calculatetotalScore()
    } else {
        savesliderValues()
    }