ios - Index out of bounds error for UIView animation method -




i getting index out of bounds error, can't figure out why. code compiles fine, error appears @ following line (animation):

import uikit  class menuviewcontroller: uiviewcontroller {      private let contentview = uiview()     private let logoview = uiimageview()     private let buttonview = uiview()     private var gamebuttons = [roundedbutton]()     private let scoreview = uiview()     private let titlelabel = uilabel()     private let recentscorelabel = uilabel()     private let highscorelabel = uilabel()      private let titles = [         "multiple choice",         "image quiz",         "right or wrong",         "emoji riddle"      ]      private var recentscores = [int]()     private var highscores = [int]()     private var scoreindex = 0     private var timer = timer()      private var midxconstraints: [nslayoutconstraint]!     private var leftconstraints: [nslayoutconstraint]!     private var rightconstraints: [nslayoutconstraint]!      override  func viewdidload() {         super.viewdidload()         navigationcontroller?.navigationbar.barstyle = .blacktranslucent         navigationcontroller?.navigationbar.tintcolor = uicolor.white         view.backgroundcolor = uicolor(red: 41/255, green: 128/255, blue: 185/255, alpha: 1.0)         layoutview()     }      override func viewwillappear(_ animated: bool) {         navigationcontroller?.navigationbar.ishidden = true     }      func updatescores() {         recentscores = [             userdefaults.standard.integer(forkey: multiplechoicerecentscoreidentifier),             userdefaults.standard.integer(forkey: imagequizrecentscoreidentifier),             userdefaults.standard.integer(forkey: rightwrongrecentscoreidentifier),             userdefaults.standard.integer(forkey: emojirecentscoreidentifier)         ]          highscores = [             userdefaults.standard.integer(forkey: multiplechoicehighscoreidentifier),             userdefaults.standard.integer(forkey: imagequizhighscoreidentifier),             userdefaults.standard.integer(forkey: rightwronghighscoreidentifier),             userdefaults.standard.integer(forkey: emojihighscoreidentifier)         ]     }      func layoutview() {         contentview.translatesautoresizingmaskintoconstraints = false         view.addsubview(contentview)          logoview.translatesautoresizingmaskintoconstraints = false         contentview.addsubview(logoview)         logoview.image = uiimage(named: "logo")          buttonview.translatesautoresizingmaskintoconstraints = false         contentview.addsubview(buttonview)          (index,title) in titles.enumerated() {             let button = roundedbutton()             button.translatesautoresizingmaskintoconstraints = false             buttonview.addsubview(button)             button.backgroundcolor = uicolor(red: 52/255, green: 152/255, blue: 219/255, alpha: 1.0)             button.titlelabel?.font = uifont.boldsystemfont(ofsize: 20)             button.settitle(title, for: .normal)             button.tag = index             gamebuttons.append(button)         }          scoreview.translatesautoresizingmaskintoconstraints = false         contentview.addsubview(scoreview)          titlelabel.translatesautoresizingmaskintoconstraints = false         recentscorelabel.translatesautoresizingmaskintoconstraints = false         highscorelabel.translatesautoresizingmaskintoconstraints = false         scoreview.addsubview(titlelabel)         scoreview.addsubview(recentscorelabel)         scoreview.addsubview(highscorelabel)          titlelabel.textalignment = .center         titlelabel.font = uifont.boldsystemfont(ofsize: 30)         titlelabel.textcolor = uicolor.white         recentscorelabel.textcolor = uicolor.white         highscorelabel.font = uifont.boldsystemfont(ofsize: 20)         highscorelabel.textcolor = uicolor.white          titlelabel.text = titles[scoreindex]         recentscorelabel.text = "recent: " + string(userdefaults.standard.integer(forkey: multiplechoicerecentscoreidentifier))         highscorelabel.text = "highscore: " + string(userdefaults.standard.integer(forkey: multiplechoicehighscoreidentifier))          let constraints = [             contentview.topanchor.constraint(equalto: toplayoutguide.bottomanchor, constant: 8.0),             contentview.leadinganchor.constraint(equalto: view.leadinganchor),             contentview.trailinganchor.constraint(equalto: view.trailinganchor),             contentview.bottomanchor.constraint(equalto: view.bottomanchor),             logoview.topanchor.constraint(equalto: contentview.topanchor, constant: 20.0),             logoview.widthanchor.constraint(equalto: contentview.widthanchor, multiplier: 0.6),             logoview.centerxanchor.constraint(equalto: contentview.centerxanchor),             logoview.heightanchor.constraint(equalto: contentview.heightanchor, multiplier: 0.2),             buttonview.topanchor.constraint(equalto: logoview.bottomanchor, constant: 20.0),             buttonview.bottomanchor.constraint(equalto: scoreview.topanchor, constant: -20.0),             buttonview.widthanchor.constraint(equalto: contentview.widthanchor, multiplier: 0.6),             buttonview.centerxanchor.constraint(equalto: contentview.centerxanchor),             gamebuttons[0].topanchor.constraint(equalto: buttonview.topanchor, constant: 8.0),             gamebuttons[0].bottomanchor.constraint(equalto: gamebuttons[1].topanchor, constant: -8.0),             gamebuttons[0].leadinganchor.constraint(equalto: buttonview.leadinganchor),             gamebuttons[0].trailinganchor.constraint(equalto: buttonview.trailinganchor),             gamebuttons[1].bottomanchor.constraint(equalto: gamebuttons[2].topanchor, constant: -8.0),             gamebuttons[1].leadinganchor.constraint(equalto: buttonview.leadinganchor),             gamebuttons[1].trailinganchor.constraint(equalto: buttonview.trailinganchor),             gamebuttons[2].bottomanchor.constraint(equalto: gamebuttons[3].topanchor, constant: -8.0),             gamebuttons[2].leadinganchor.constraint(equalto: buttonview.leadinganchor),             gamebuttons[2].trailinganchor.constraint(equalto: buttonview.trailinganchor),             gamebuttons[3].bottomanchor.constraint(equalto: buttonview.bottomanchor, constant: -8.0),             gamebuttons[3].leadinganchor.constraint(equalto: buttonview.leadinganchor),             gamebuttons[3].trailinganchor.constraint(equalto: buttonview.trailinganchor),             gamebuttons[0].heightanchor.constraint(equalto: gamebuttons[1].heightanchor),             gamebuttons[1].heightanchor.constraint(equalto: gamebuttons[2].heightanchor),             gamebuttons[2].heightanchor.constraint(equalto: gamebuttons[3].heightanchor),             scoreview.bottomanchor.constraint(equalto: contentview.bottomanchor, constant: -40.0),             scoreview.widthanchor.constraint(equalto: contentview.widthanchor, multiplier: 0.6),             scoreview.heightanchor.constraint(equalto: contentview.heightanchor, multiplier: 0.3),             titlelabel.topanchor.constraint(equalto: scoreview.topanchor, constant: 8.0),             titlelabel.leadinganchor.constraint(equalto: scoreview.leadinganchor),             titlelabel.trailinganchor.constraint(equalto: scoreview.trailinganchor),             titlelabel.bottomanchor.constraint(equalto: recentscorelabel.topanchor, constant: -8.0),             recentscorelabel.leadinganchor.constraint(equalto: scoreview.leadinganchor),             recentscorelabel.trailinganchor.constraint(equalto: scoreview.trailinganchor),             recentscorelabel.bottomanchor.constraint(equalto: highscorelabel.topanchor, constant: -8.0),             highscorelabel.leadinganchor.constraint(equalto: scoreview.leadinganchor),             highscorelabel.trailinganchor.constraint(equalto: scoreview.trailinganchor),             highscorelabel.bottomanchor.constraint(equalto: scoreview.bottomanchor, constant: -8.0),             titlelabel.heightanchor.constraint(equalto: recentscorelabel.heightanchor),             recentscorelabel.heightanchor.constraint(equalto: highscorelabel.heightanchor)         ]          midxconstraints = [scoreview.centerxanchor.constraint(equalto: contentview.centerxanchor)]         leftconstraints = [scoreview.trailinganchor.constraint(equalto: contentview.leadinganchor)]         rightconstraints = [scoreview.leadinganchor.constraint(equalto: contentview.trailinganchor)]          nslayoutconstraint.activate(constraints)         nslayoutconstraint.activate(midxconstraints)          timer = timer.scheduledtimer(timeinterval: 3.0, target: self, selector: #selector(nextscores), userinfo: nil, repeats: true)     }      func nextscores() {         scoreindex = scoreindex < (recentscores.count - 1) ? scoreindex + 1 : 0         uiview.animate(withduration: 1.0, animations: {             nslayoutconstraint.deactivate(self.midxconstraints)             nslayoutconstraint.activate(self.leftconstraints)             self.view.layoutifneeded()         }) {(completion: bool) in             self.titlelabel.text = self.titles[self.scoreindex]             self.recentscorelabel.text  = "recent: " + string(self.recentscores[self.scoreindex]) //index out of bounds error appears here             self.highscorelabel.text = "highscore: " + string(self.highscores[self.scoreindex])             nslayoutconstraint.deactivate(self.leftconstraints)             nslayoutconstraint.activate(self.rightconstraints)             self.view.layoutifneeded()             uiview.animate(withduration: 1.0, animations: {                 nslayoutconstraint.deactivate(self.rightconstraints)                 nslayoutconstraint.activate(self.midxconstraints)                 self.view.layoutifneeded()             })         }     }    } 

the recentscores empty array, i.e. has 0 elements, , trying access first element. don't append element array. that's why "index out of bounds" error.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -