2016-05-01 19 views
2

初心者、初回スウィフトアプリ。 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) 
    } 

} 
+0

'incrementIndex'では、index 3 – Paulw11

答えて

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() 
    } 
+0

'if index Paulw11

+0

はい、確かです。 OPに読めるものは何でも。私は 'currentQuestion3()'メソッドと同じ構文を使用して理解しやすくしようとしました。 – Shubhank

+0

ありがとうShubhank/Paul! 私は両方の提案を試してみましたが、どちらもうまくいきました。私はちょうどカウントの後にシュバンクの答えを-3に修正しなければならなかった。 – popetec

関連する問題